TDengine/source/libs/nodes/src/nodesToSQLFuncs.c

234 lines
6.5 KiB
C
Raw Normal View History

2022-03-29 11:51:11 +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 "cmdnodes.h"
#include "nodesUtil.h"
#include "plannodes.h"
#include "querynodes.h"
#include "taos.h"
#include "taoserror.h"
#include "thash.h"
const char *operatorTypeStr(EOperatorType type) {
switch (type) {
case OP_TYPE_ADD:
return "+";
case OP_TYPE_SUB:
return "-";
case OP_TYPE_MULTI:
return "*";
case OP_TYPE_DIV:
return "/";
case OP_TYPE_REM:
return "%";
case OP_TYPE_MINUS:
return "-";
case OP_TYPE_BIT_AND:
return "&";
case OP_TYPE_BIT_OR:
return "|";
case OP_TYPE_GREATER_THAN:
return ">";
case OP_TYPE_GREATER_EQUAL:
return ">=";
case OP_TYPE_LOWER_THAN:
return "<";
case OP_TYPE_LOWER_EQUAL:
return "<=";
case OP_TYPE_EQUAL:
return "=";
case OP_TYPE_NOT_EQUAL:
return "<>";
case OP_TYPE_IN:
return "IN";
case OP_TYPE_NOT_IN:
return "NOT IN";
case OP_TYPE_LIKE:
return "LIKE";
case OP_TYPE_NOT_LIKE:
return "NOT LIKE";
case OP_TYPE_MATCH:
return "MATCH";
case OP_TYPE_NMATCH:
return "NMATCH";
case OP_TYPE_IS_NULL:
return "IS NULL";
case OP_TYPE_IS_NOT_NULL:
return "IS NOT NULL";
case OP_TYPE_IS_TRUE:
return "IS TRUE";
case OP_TYPE_IS_FALSE:
return "IS FALSE";
case OP_TYPE_IS_UNKNOWN:
return "IS UNKNOWN";
case OP_TYPE_IS_NOT_TRUE:
return "IS NOT TRUE";
case OP_TYPE_IS_NOT_FALSE:
return "IS NOT FALSE";
case OP_TYPE_IS_NOT_UNKNOWN:
return "IS NOT UNKNOWN";
case OP_TYPE_JSON_GET_VALUE:
return "=>";
case OP_TYPE_JSON_CONTAINS:
return "CONTAINS";
case OP_TYPE_ASSIGN:
return "=";
default:
break;
}
return "UNKNOWN";
}
const char *logicConditionTypeStr(ELogicConditionType type) {
switch (type) {
case LOGIC_COND_TYPE_AND:
return "AND";
case LOGIC_COND_TYPE_OR:
return "OR";
case LOGIC_COND_TYPE_NOT:
return "NOT";
default:
break;
}
return "UNKNOWN";
}
2022-03-29 11:51:11 +00:00
int32_t nodesNodeToSQL(SNode *pNode, char *buf, int32_t bufSize, int32_t *len) {
switch (pNode->type) {
case QUERY_NODE_COLUMN: {
SColumnNode *colNode = (SColumnNode *)pNode;
if (colNode->dbName[0]) {
2022-04-01 02:29:20 +00:00
*len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->dbName);
2022-03-29 11:51:11 +00:00
}
2022-04-26 03:50:35 +00:00
2022-03-29 11:51:11 +00:00
if (colNode->tableAlias[0]) {
2022-04-01 02:29:20 +00:00
*len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableAlias);
2022-03-29 11:51:11 +00:00
} else if (colNode->tableName[0]) {
2022-04-01 02:29:20 +00:00
*len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableName);
2022-03-29 11:51:11 +00:00
}
2022-04-26 03:50:35 +00:00
2022-04-09 08:14:01 +00:00
if (colNode->tableAlias[0]) {
*len += snprintf(buf + *len, bufSize - *len, "`%s`", colNode->node.userAlias);
2022-04-09 08:14:01 +00:00
} else {
*len += snprintf(buf + *len, bufSize - *len, "%s", colNode->node.userAlias);
2022-04-09 08:14:01 +00:00
}
2022-04-26 03:50:35 +00:00
2022-03-29 11:51:11 +00:00
return TSDB_CODE_SUCCESS;
}
2022-04-26 03:50:35 +00:00
case QUERY_NODE_VALUE: {
2022-03-29 11:51:11 +00:00
SValueNode *colNode = (SValueNode *)pNode;
2022-04-26 03:50:35 +00:00
char *t = nodesGetStrValueFromNode(colNode);
2022-03-29 11:51:11 +00:00
if (NULL == t) {
nodesError("fail to get str value from valueNode");
2022-12-03 02:03:18 +00:00
NODES_ERR_RET(TSDB_CODE_APP_ERROR);
2022-03-29 11:51:11 +00:00
}
2022-04-26 03:50:35 +00:00
2022-08-25 03:49:01 +00:00
int32_t tlen = strlen(t);
if (tlen > 32) {
*len += snprintf(buf + *len, bufSize - *len, "%.*s...%s", 32, t, t + tlen - 1);
} else {
*len += snprintf(buf + *len, bufSize - *len, "%s", t);
}
2022-03-29 11:51:11 +00:00
taosMemoryFree(t);
2022-04-26 03:50:35 +00:00
2022-03-29 11:51:11 +00:00
return TSDB_CODE_SUCCESS;
}
case QUERY_NODE_OPERATOR: {
2022-04-26 03:50:35 +00:00
SOperatorNode *pOpNode = (SOperatorNode *)pNode;
2022-04-01 02:29:20 +00:00
*len += snprintf(buf + *len, bufSize - *len, "(");
2022-03-29 11:51:11 +00:00
if (pOpNode->pLeft) {
2022-03-30 09:09:30 +00:00
NODES_ERR_RET(nodesNodeToSQL(pOpNode->pLeft, buf, bufSize, len));
2022-03-29 11:51:11 +00:00
}
*len += snprintf(buf + *len, bufSize - *len, " %s ", operatorTypeStr(pOpNode->opType));
2022-03-29 11:51:11 +00:00
if (pOpNode->pRight) {
2022-03-30 09:09:30 +00:00
NODES_ERR_RET(nodesNodeToSQL(pOpNode->pRight, buf, bufSize, len));
2022-04-26 03:50:35 +00:00
}
2022-03-29 11:51:11 +00:00
2022-04-01 02:29:20 +00:00
*len += snprintf(buf + *len, bufSize - *len, ")");
2022-03-29 11:51:11 +00:00
return TSDB_CODE_SUCCESS;
}
2022-04-26 03:50:35 +00:00
case QUERY_NODE_LOGIC_CONDITION: {
SLogicConditionNode *pLogicNode = (SLogicConditionNode *)pNode;
SNode *node = NULL;
bool first = true;
2022-04-01 02:29:20 +00:00
*len += snprintf(buf + *len, bufSize - *len, "(");
2022-04-26 03:50:35 +00:00
2022-03-29 11:51:11 +00:00
FOREACH(node, pLogicNode->pParameterList) {
if (!first) {
*len += snprintf(buf + *len, bufSize - *len, " %s ", logicConditionTypeStr(pLogicNode->condType));
2022-03-29 11:51:11 +00:00
}
2022-03-30 09:09:30 +00:00
NODES_ERR_RET(nodesNodeToSQL(node, buf, bufSize, len));
2022-03-29 11:51:11 +00:00
first = false;
}
2022-04-01 02:29:20 +00:00
*len += snprintf(buf + *len, bufSize - *len, ")");
2022-03-29 11:51:11 +00:00
return TSDB_CODE_SUCCESS;
}
2022-04-26 03:50:35 +00:00
case QUERY_NODE_FUNCTION: {
SFunctionNode *pFuncNode = (SFunctionNode *)pNode;
SNode *node = NULL;
bool first = true;
2022-04-01 02:29:20 +00:00
*len += snprintf(buf + *len, bufSize - *len, "%s(", pFuncNode->functionName);
2022-04-26 03:50:35 +00:00
2022-03-29 11:51:11 +00:00
FOREACH(node, pFuncNode->pParameterList) {
if (!first) {
2022-04-01 02:29:20 +00:00
*len += snprintf(buf + *len, bufSize - *len, ", ");
2022-03-29 11:51:11 +00:00
}
2022-03-30 09:09:30 +00:00
NODES_ERR_RET(nodesNodeToSQL(node, buf, bufSize, len));
2022-03-29 11:51:11 +00:00
first = false;
}
2022-04-01 02:29:20 +00:00
*len += snprintf(buf + *len, bufSize - *len, ")");
2022-03-29 11:51:11 +00:00
return TSDB_CODE_SUCCESS;
}
2022-04-26 03:50:35 +00:00
case QUERY_NODE_NODE_LIST: {
SNodeListNode *pListNode = (SNodeListNode *)pNode;
SNode *node = NULL;
bool first = true;
2022-08-25 03:49:01 +00:00
int32_t num = 0;
2022-04-26 03:50:35 +00:00
2022-04-01 02:29:20 +00:00
*len += snprintf(buf + *len, bufSize - *len, "(");
2022-04-26 03:50:35 +00:00
2022-03-30 09:09:30 +00:00
FOREACH(node, pListNode->pNodeList) {
if (!first) {
2022-04-01 02:29:20 +00:00
*len += snprintf(buf + *len, bufSize - *len, ", ");
2022-08-25 03:49:01 +00:00
if (++num >= 10) {
*len += snprintf(buf + *len, bufSize - *len, "...");
break;
}
2022-03-30 09:09:30 +00:00
}
NODES_ERR_RET(nodesNodeToSQL(node, buf, bufSize, len));
first = false;
}
2022-04-26 03:50:35 +00:00
2022-04-01 02:29:20 +00:00
*len += snprintf(buf + *len, bufSize - *len, ")");
2022-03-29 11:51:11 +00:00
return TSDB_CODE_SUCCESS;
}
default:
break;
}
2022-04-26 03:50:35 +00:00
2022-03-29 11:51:11 +00:00
nodesError("nodesNodeToSQL unknown node = %s", nodesNodeName(pNode->type));
2022-12-03 02:03:18 +00:00
NODES_RET(TSDB_CODE_APP_ERROR);
2022-03-29 11:51:11 +00:00
}