nodesToSQLFuncs.c 6.5 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
      if (colNode->tableAlias[0]) {
123
        *len += snprintf(buf + *len, bufSize - *len, "`%s`", colNode->node.userAlias);
D
dapan1121 已提交
124
      } else {
125
        *len += snprintf(buf + *len, bufSize - *len, "%s", colNode->node.userAlias);
D
dapan1121 已提交
126
      }
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");
S
Shengliang Guan 已提交
135
        NODES_ERR_RET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
136
      }
X
Xiaoyu Wang 已提交
137

D
dapan1121 已提交
138 139 140 141 142 143
      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);
      }
D
dapan1121 已提交
144
      taosMemoryFree(t);
X
Xiaoyu Wang 已提交
145

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

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

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

D
dapan1121 已提交
161
      *len += snprintf(buf + *len, bufSize - *len, ")");
D
dapan1121 已提交
162 163 164

      return TSDB_CODE_SUCCESS;
    }
X
Xiaoyu Wang 已提交
165 166 167 168 169
    case QUERY_NODE_LOGIC_CONDITION: {
      SLogicConditionNode *pLogicNode = (SLogicConditionNode *)pNode;
      SNode               *node = NULL;
      bool                 first = true;

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

D
dapan1121 已提交
172 173
      FOREACH(node, pLogicNode->pParameterList) {
        if (!first) {
174
          *len += snprintf(buf + *len, bufSize - *len, " %s ", logicConditionTypeStr(pLogicNode->condType));
D
dapan1121 已提交
175
        }
D
dapan1121 已提交
176
        NODES_ERR_RET(nodesNodeToSQL(node, buf, bufSize, len));
D
dapan1121 已提交
177 178 179
        first = false;
      }

D
dapan1121 已提交
180
      *len += snprintf(buf + *len, bufSize - *len, ")");
D
dapan1121 已提交
181 182 183

      return TSDB_CODE_SUCCESS;
    }
X
Xiaoyu Wang 已提交
184 185 186 187 188
    case QUERY_NODE_FUNCTION: {
      SFunctionNode *pFuncNode = (SFunctionNode *)pNode;
      SNode         *node = NULL;
      bool           first = true;

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

D
dapan1121 已提交
191 192
      FOREACH(node, pFuncNode->pParameterList) {
        if (!first) {
D
dapan1121 已提交
193
          *len += snprintf(buf + *len, bufSize - *len, ", ");
D
dapan1121 已提交
194
        }
D
dapan1121 已提交
195
        NODES_ERR_RET(nodesNodeToSQL(node, buf, bufSize, len));
D
dapan1121 已提交
196 197 198
        first = false;
      }

D
dapan1121 已提交
199
      *len += snprintf(buf + *len, bufSize - *len, ")");
D
dapan1121 已提交
200 201 202

      return TSDB_CODE_SUCCESS;
    }
X
Xiaoyu Wang 已提交
203 204 205 206
    case QUERY_NODE_NODE_LIST: {
      SNodeListNode *pListNode = (SNodeListNode *)pNode;
      SNode         *node = NULL;
      bool           first = true;
D
dapan1121 已提交
207
      int32_t        num = 0;
X
Xiaoyu Wang 已提交
208

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

D
dapan1121 已提交
211 212
      FOREACH(node, pListNode->pNodeList) {
        if (!first) {
D
dapan1121 已提交
213
          *len += snprintf(buf + *len, bufSize - *len, ", ");
D
dapan1121 已提交
214 215 216 217
          if (++num >= 10) {
            *len += snprintf(buf + *len, bufSize - *len, "...");
            break;
          }
D
dapan1121 已提交
218 219 220 221
        }
        NODES_ERR_RET(nodesNodeToSQL(node, buf, bufSize, len));
        first = false;
      }
X
Xiaoyu Wang 已提交
222

D
dapan1121 已提交
223
      *len += snprintf(buf + *len, bufSize - *len, ")");
D
dapan1121 已提交
224 225 226 227 228 229

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

D
dapan1121 已提交
231
  nodesError("nodesNodeToSQL unknown node = %s", nodesNodeName(pNode->type));
S
Shengliang Guan 已提交
232
  NODES_RET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
233
}