nodesToSQLFuncs.c 6.2 KB
Newer Older
D
dapan1121 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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"

24
const char *operatorTypeStr(EOperatorType type) {
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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
  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";
}

94
const char *logicConditionTypeStr(ELogicConditionType type) {
95 96 97 98 99 100 101 102 103 104 105 106
  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";
}
D
dapan1121 已提交
107 108 109 110 111 112

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]) {
D
dapan1121 已提交
113
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->dbName);
D
dapan1121 已提交
114
      }
X
Xiaoyu Wang 已提交
115

D
dapan1121 已提交
116
      if (colNode->tableAlias[0]) {
D
dapan1121 已提交
117
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableAlias);
D
dapan1121 已提交
118
      } else if (colNode->tableName[0]) {
D
dapan1121 已提交
119
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableName);
D
dapan1121 已提交
120
      }
X
Xiaoyu Wang 已提交
121

D
dapan1121 已提交
122 123 124 125 126
      if (colNode->tableAlias[0]) {
        *len += snprintf(buf + *len, bufSize - *len, "`%s`", colNode->colName);
      } else {
        *len += snprintf(buf + *len, bufSize - *len, "%s", colNode->colName);
      }
X
Xiaoyu Wang 已提交
127

D
dapan1121 已提交
128 129
      return TSDB_CODE_SUCCESS;
    }
X
Xiaoyu Wang 已提交
130
    case QUERY_NODE_VALUE: {
D
dapan1121 已提交
131
      SValueNode *colNode = (SValueNode *)pNode;
X
Xiaoyu Wang 已提交
132
      char       *t = nodesGetStrValueFromNode(colNode);
D
dapan1121 已提交
133 134
      if (NULL == t) {
        nodesError("fail to get str value from valueNode");
D
dapan1121 已提交
135
        NODES_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
D
dapan1121 已提交
136
      }
X
Xiaoyu Wang 已提交
137

D
dapan1121 已提交
138
      *len += snprintf(buf + *len, bufSize - *len, "%s", t);
D
dapan1121 已提交
139
      taosMemoryFree(t);
X
Xiaoyu Wang 已提交
140

D
dapan1121 已提交
141 142 143
      return TSDB_CODE_SUCCESS;
    }
    case QUERY_NODE_OPERATOR: {
X
Xiaoyu Wang 已提交
144
      SOperatorNode *pOpNode = (SOperatorNode *)pNode;
D
dapan1121 已提交
145
      *len += snprintf(buf + *len, bufSize - *len, "(");
D
dapan1121 已提交
146
      if (pOpNode->pLeft) {
D
dapan1121 已提交
147
        NODES_ERR_RET(nodesNodeToSQL(pOpNode->pLeft, buf, bufSize, len));
D
dapan1121 已提交
148 149
      }

150
      *len += snprintf(buf + *len, bufSize - *len, " %s ", operatorTypeStr(pOpNode->opType));
D
dapan1121 已提交
151 152

      if (pOpNode->pRight) {
D
dapan1121 已提交
153
        NODES_ERR_RET(nodesNodeToSQL(pOpNode->pRight, buf, bufSize, len));
X
Xiaoyu Wang 已提交
154
      }
D
dapan1121 已提交
155

D
dapan1121 已提交
156
      *len += snprintf(buf + *len, bufSize - *len, ")");
D
dapan1121 已提交
157 158 159

      return TSDB_CODE_SUCCESS;
    }
X
Xiaoyu Wang 已提交
160 161 162 163 164
    case QUERY_NODE_LOGIC_CONDITION: {
      SLogicConditionNode *pLogicNode = (SLogicConditionNode *)pNode;
      SNode               *node = NULL;
      bool                 first = true;

D
dapan1121 已提交
165
      *len += snprintf(buf + *len, bufSize - *len, "(");
X
Xiaoyu Wang 已提交
166

D
dapan1121 已提交
167 168
      FOREACH(node, pLogicNode->pParameterList) {
        if (!first) {
169
          *len += snprintf(buf + *len, bufSize - *len, " %s ", logicConditionTypeStr(pLogicNode->condType));
D
dapan1121 已提交
170
        }
D
dapan1121 已提交
171
        NODES_ERR_RET(nodesNodeToSQL(node, buf, bufSize, len));
D
dapan1121 已提交
172 173 174
        first = false;
      }

D
dapan1121 已提交
175
      *len += snprintf(buf + *len, bufSize - *len, ")");
D
dapan1121 已提交
176 177 178

      return TSDB_CODE_SUCCESS;
    }
X
Xiaoyu Wang 已提交
179 180 181 182 183
    case QUERY_NODE_FUNCTION: {
      SFunctionNode *pFuncNode = (SFunctionNode *)pNode;
      SNode         *node = NULL;
      bool           first = true;

D
dapan1121 已提交
184
      *len += snprintf(buf + *len, bufSize - *len, "%s(", pFuncNode->functionName);
X
Xiaoyu Wang 已提交
185

D
dapan1121 已提交
186 187
      FOREACH(node, pFuncNode->pParameterList) {
        if (!first) {
D
dapan1121 已提交
188
          *len += snprintf(buf + *len, bufSize - *len, ", ");
D
dapan1121 已提交
189
        }
D
dapan1121 已提交
190
        NODES_ERR_RET(nodesNodeToSQL(node, buf, bufSize, len));
D
dapan1121 已提交
191 192 193
        first = false;
      }

D
dapan1121 已提交
194
      *len += snprintf(buf + *len, bufSize - *len, ")");
D
dapan1121 已提交
195 196 197

      return TSDB_CODE_SUCCESS;
    }
X
Xiaoyu Wang 已提交
198 199 200 201 202
    case QUERY_NODE_NODE_LIST: {
      SNodeListNode *pListNode = (SNodeListNode *)pNode;
      SNode         *node = NULL;
      bool           first = true;

D
dapan1121 已提交
203
      *len += snprintf(buf + *len, bufSize - *len, "(");
X
Xiaoyu Wang 已提交
204

D
dapan1121 已提交
205 206
      FOREACH(node, pListNode->pNodeList) {
        if (!first) {
D
dapan1121 已提交
207
          *len += snprintf(buf + *len, bufSize - *len, ", ");
D
dapan1121 已提交
208 209 210 211
        }
        NODES_ERR_RET(nodesNodeToSQL(node, buf, bufSize, len));
        first = false;
      }
X
Xiaoyu Wang 已提交
212

D
dapan1121 已提交
213
      *len += snprintf(buf + *len, bufSize - *len, ")");
D
dapan1121 已提交
214 215 216 217 218 219

      return TSDB_CODE_SUCCESS;
    }
    default:
      break;
  }
X
Xiaoyu Wang 已提交
220

D
dapan1121 已提交
221
  nodesError("nodesNodeToSQL unknown node = %s", nodesNodeName(pNode->type));
D
dapan1121 已提交
222
  NODES_RET(TSDB_CODE_QRY_APP_ERROR);
D
dapan1121 已提交
223
}