/src/tdengine/source/libs/nodes/src/nodesToSQLFuncs.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com> |
3 | | * |
4 | | * This program is free software: you can use, redistribute, and/or modify |
5 | | * it under the terms of the GNU Affero General Public License, version 3 |
6 | | * or later ("AGPL"), as published by the Free Software Foundation. |
7 | | * |
8 | | * This program is distributed in the hope that it will be useful, but WITHOUT |
9 | | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
10 | | * FITNESS FOR A PARTICULAR PURPOSE. |
11 | | * |
12 | | * You should have received a copy of the GNU Affero General Public License |
13 | | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
14 | | */ |
15 | | |
16 | | #include "cmdnodes.h" |
17 | | #include "nodesUtil.h" |
18 | | #include "plannodes.h" |
19 | | #include "querynodes.h" |
20 | | #include "taos.h" |
21 | | #include "taoserror.h" |
22 | | #include "thash.h" |
23 | | |
24 | 0 | const char *operatorTypeStr(EOperatorType type) { |
25 | 0 | switch (type) { |
26 | 0 | case OP_TYPE_ADD: |
27 | 0 | return "+"; |
28 | 0 | case OP_TYPE_SUB: |
29 | 0 | return "-"; |
30 | 0 | case OP_TYPE_MULTI: |
31 | 0 | return "*"; |
32 | 0 | case OP_TYPE_DIV: |
33 | 0 | return "/"; |
34 | 0 | case OP_TYPE_REM: |
35 | 0 | return "%"; |
36 | 0 | case OP_TYPE_MINUS: |
37 | 0 | return "-"; |
38 | 0 | case OP_TYPE_BIT_AND: |
39 | 0 | return "&"; |
40 | 0 | case OP_TYPE_BIT_OR: |
41 | 0 | return "|"; |
42 | 0 | case OP_TYPE_GREATER_THAN: |
43 | 0 | return ">"; |
44 | 0 | case OP_TYPE_GREATER_EQUAL: |
45 | 0 | return ">="; |
46 | 0 | case OP_TYPE_LOWER_THAN: |
47 | 0 | return "<"; |
48 | 0 | case OP_TYPE_LOWER_EQUAL: |
49 | 0 | return "<="; |
50 | 0 | case OP_TYPE_EQUAL: |
51 | 0 | return "="; |
52 | 0 | case OP_TYPE_NOT_EQUAL: |
53 | 0 | return "<>"; |
54 | 0 | case OP_TYPE_IN: |
55 | 0 | return "IN"; |
56 | 0 | case OP_TYPE_NOT_IN: |
57 | 0 | return "NOT IN"; |
58 | 0 | case OP_TYPE_LIKE: |
59 | 0 | return "LIKE"; |
60 | 0 | case OP_TYPE_NOT_LIKE: |
61 | 0 | return "NOT LIKE"; |
62 | 0 | case OP_TYPE_MATCH: |
63 | 0 | return "MATCH"; |
64 | 0 | case OP_TYPE_NMATCH: |
65 | 0 | return "NMATCH"; |
66 | 0 | case OP_TYPE_IS_NULL: |
67 | 0 | return "IS NULL"; |
68 | 0 | case OP_TYPE_IS_NOT_NULL: |
69 | 0 | return "IS NOT NULL"; |
70 | 0 | case OP_TYPE_IS_TRUE: |
71 | 0 | return "IS TRUE"; |
72 | 0 | case OP_TYPE_IS_FALSE: |
73 | 0 | return "IS FALSE"; |
74 | 0 | case OP_TYPE_IS_UNKNOWN: |
75 | 0 | return "IS UNKNOWN"; |
76 | 0 | case OP_TYPE_IS_NOT_TRUE: |
77 | 0 | return "IS NOT TRUE"; |
78 | 0 | case OP_TYPE_IS_NOT_FALSE: |
79 | 0 | return "IS NOT FALSE"; |
80 | 0 | case OP_TYPE_IS_NOT_UNKNOWN: |
81 | 0 | return "IS NOT UNKNOWN"; |
82 | 0 | case OP_TYPE_EXISTS: |
83 | 0 | return "EXISTS"; |
84 | 0 | case OP_TYPE_NOT_EXISTS: |
85 | 0 | return "NOT EXISTS"; |
86 | 0 | case OP_TYPE_JSON_GET_VALUE: |
87 | 0 | return "=>"; |
88 | 0 | case OP_TYPE_JSON_CONTAINS: |
89 | 0 | return "CONTAINS"; |
90 | 0 | case OP_TYPE_ASSIGN: |
91 | 0 | return "="; |
92 | 0 | default: |
93 | 0 | break; |
94 | 0 | } |
95 | 0 | return "UNKNOWN"; |
96 | 0 | } |
97 | | |
98 | 0 | const char *operatorTypeStrEx(EOperatorType type, bool isNegative) { |
99 | 0 | if (isNegative) { |
100 | 0 | switch (type) { |
101 | 0 | case OP_TYPE_IN: |
102 | 0 | return "!= ANY"; |
103 | 0 | case OP_TYPE_NOT_IN: |
104 | 0 | return "= ALL"; |
105 | 0 | default: |
106 | 0 | break; |
107 | 0 | } |
108 | 0 | return "UNKNOWN"; |
109 | 0 | } |
110 | | |
111 | 0 | return operatorTypeStr(type); |
112 | 0 | } |
113 | | |
114 | 0 | const char *logicConditionTypeStr(ELogicConditionType type) { |
115 | 0 | switch (type) { |
116 | 0 | case LOGIC_COND_TYPE_AND: |
117 | 0 | return "AND"; |
118 | 0 | case LOGIC_COND_TYPE_OR: |
119 | 0 | return "OR"; |
120 | 0 | case LOGIC_COND_TYPE_NOT: |
121 | 0 | return "NOT"; |
122 | 0 | default: |
123 | 0 | break; |
124 | 0 | } |
125 | 0 | return "UNKNOWN"; |
126 | 0 | } |
127 | | |
128 | 0 | int32_t nodesNodeToSQL(SNode *pNode, char *buf, int32_t bufSize, int32_t *len) { |
129 | 0 | return nodesNodeToSQLFormat(pNode, buf, bufSize, len, false); |
130 | 0 | } |
131 | | |
132 | 0 | int32_t nodesNodeToSQLFormat(SNode *pNode, char *buf, int32_t bufSize, int32_t *len, bool longFormat) { |
133 | 0 | switch (pNode->type) { |
134 | 0 | case QUERY_NODE_COLUMN: { |
135 | 0 | SColumnNode *colNode = (SColumnNode *)pNode; |
136 | 0 | if (colNode->dbName[0]) { |
137 | 0 | *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->dbName); |
138 | 0 | } |
139 | |
|
140 | 0 | if (colNode->tableAlias[0]) { |
141 | 0 | *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableAlias); |
142 | 0 | } else if (colNode->tableName[0]) { |
143 | 0 | *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableName); |
144 | 0 | } |
145 | |
|
146 | 0 | if (colNode->node.userAlias[0]) { |
147 | 0 | *len += snprintf(buf + *len, bufSize - *len, "`%s`", colNode->node.userAlias); |
148 | 0 | } else { |
149 | 0 | *len += snprintf(buf + *len, bufSize - *len, "`%s`", colNode->colName); |
150 | 0 | } |
151 | |
|
152 | 0 | return TSDB_CODE_SUCCESS; |
153 | 0 | } |
154 | 0 | case QUERY_NODE_VALUE: { |
155 | 0 | SValueNode *colNode = (SValueNode *)pNode; |
156 | 0 | char *t = nodesGetStrValueFromNode(colNode); |
157 | 0 | if (NULL == t) { |
158 | | // NULL type value (e.g. ELSE NULL in CASE WHEN) |
159 | 0 | *len += tsnprintf(buf + *len, bufSize - *len, "NULL"); |
160 | 0 | return TSDB_CODE_SUCCESS; |
161 | 0 | } |
162 | | |
163 | 0 | int32_t tlen = strlen(t); |
164 | 0 | if (longFormat) { |
165 | 0 | *len += snprintf(buf + *len, bufSize - *len, "%s", t); |
166 | 0 | } else { |
167 | 0 | if (tlen > 32) { |
168 | 0 | *len += snprintf(buf + *len, bufSize - *len, "%.*s...%s", 32, t, t + tlen - 1); |
169 | 0 | } else { |
170 | 0 | *len += snprintf(buf + *len, bufSize - *len, "%s", t); |
171 | 0 | } |
172 | 0 | } |
173 | |
|
174 | 0 | taosMemoryFree(t); |
175 | |
|
176 | 0 | return TSDB_CODE_SUCCESS; |
177 | 0 | } |
178 | 0 | case QUERY_NODE_OPERATOR: { |
179 | 0 | SOperatorNode *pOpNode = (SOperatorNode *)pNode; |
180 | 0 | *len += snprintf(buf + *len, bufSize - *len, "("); |
181 | 0 | if (pOpNode->pLeft) { |
182 | 0 | NODES_ERR_RET(nodesNodeToSQLFormat(pOpNode->pLeft, buf, bufSize, len, true)); |
183 | 0 | } |
184 | | |
185 | 0 | *len += tsnprintf(buf + *len, bufSize - *len, " %s ", operatorTypeStrEx(pOpNode->opType, pOpNode->flag & OPERATOR_FLAG_NEGATIVE_OP)); |
186 | |
|
187 | 0 | if (pOpNode->pRight) { |
188 | 0 | NODES_ERR_RET(nodesNodeToSQLFormat(pOpNode->pRight, buf, bufSize, len, true)); |
189 | 0 | } |
190 | | |
191 | 0 | *len += snprintf(buf + *len, bufSize - *len, ")"); |
192 | |
|
193 | 0 | return TSDB_CODE_SUCCESS; |
194 | 0 | } |
195 | 0 | case QUERY_NODE_LOGIC_CONDITION: { |
196 | 0 | SLogicConditionNode *pLogicNode = (SLogicConditionNode *)pNode; |
197 | 0 | SNode *node = NULL; |
198 | 0 | bool first = true; |
199 | |
|
200 | 0 | *len += snprintf(buf + *len, bufSize - *len, "("); |
201 | |
|
202 | 0 | FOREACH(node, pLogicNode->pParameterList) { |
203 | 0 | if (!first) { |
204 | 0 | *len += snprintf(buf + *len, bufSize - *len, " %s ", logicConditionTypeStr(pLogicNode->condType)); |
205 | 0 | } |
206 | 0 | NODES_ERR_RET(nodesNodeToSQLFormat(node, buf, bufSize, len, true)); |
207 | 0 | first = false; |
208 | 0 | } |
209 | | |
210 | 0 | *len += snprintf(buf + *len, bufSize - *len, ")"); |
211 | |
|
212 | 0 | return TSDB_CODE_SUCCESS; |
213 | 0 | } |
214 | 0 | case QUERY_NODE_FUNCTION: { |
215 | 0 | SFunctionNode *pFuncNode = (SFunctionNode *)pNode; |
216 | 0 | SNode *node = NULL; |
217 | 0 | bool first = true; |
218 | |
|
219 | 0 | *len += snprintf(buf + *len, bufSize - *len, "%s(", pFuncNode->functionName); |
220 | |
|
221 | 0 | FOREACH(node, pFuncNode->pParameterList) { |
222 | 0 | if (!first) { |
223 | 0 | *len += snprintf(buf + *len, bufSize - *len, ", "); |
224 | 0 | } |
225 | 0 | NODES_ERR_RET(nodesNodeToSQLFormat(node, buf, bufSize, len, true)); |
226 | 0 | first = false; |
227 | 0 | } |
228 | | |
229 | 0 | *len += snprintf(buf + *len, bufSize - *len, ")"); |
230 | |
|
231 | 0 | return TSDB_CODE_SUCCESS; |
232 | 0 | } |
233 | 0 | case QUERY_NODE_NODE_LIST: { |
234 | 0 | SNodeListNode *pListNode = (SNodeListNode *)pNode; |
235 | 0 | SNode *node = NULL; |
236 | 0 | bool first = true; |
237 | 0 | int32_t num = 0; |
238 | |
|
239 | 0 | *len += snprintf(buf + *len, bufSize - *len, "("); |
240 | |
|
241 | 0 | FOREACH(node, pListNode->pNodeList) { |
242 | 0 | if (!first) { |
243 | 0 | *len += snprintf(buf + *len, bufSize - *len, ", "); |
244 | 0 | if (!longFormat) { |
245 | 0 | if (++num >= 10) { |
246 | 0 | *len += snprintf(buf + *len, bufSize - *len, "..."); |
247 | 0 | break; |
248 | 0 | } |
249 | 0 | } |
250 | 0 | } |
251 | 0 | NODES_ERR_RET(nodesNodeToSQLFormat(node, buf, bufSize, len, true)); |
252 | 0 | first = false; |
253 | 0 | } |
254 | | |
255 | 0 | *len += snprintf(buf + *len, bufSize - *len, ")"); |
256 | |
|
257 | 0 | return TSDB_CODE_SUCCESS; |
258 | 0 | } |
259 | 0 | case QUERY_NODE_REMOTE_VALUE: { |
260 | 0 | SRemoteValueNode* pRemote = (SRemoteValueNode*)pNode; |
261 | 0 | *len += snprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1); |
262 | 0 | return TSDB_CODE_SUCCESS; |
263 | 0 | } |
264 | 0 | case QUERY_NODE_REMOTE_VALUE_LIST: { |
265 | 0 | SRemoteValueListNode* pRemote = (SRemoteValueListNode*)pNode; |
266 | 0 | *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1); |
267 | 0 | return TSDB_CODE_SUCCESS; |
268 | 0 | } |
269 | 0 | case QUERY_NODE_REMOTE_ROW: { |
270 | 0 | SRemoteRowNode* pRemote = (SRemoteRowNode*)pNode; |
271 | 0 | *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1); |
272 | 0 | return TSDB_CODE_SUCCESS; |
273 | 0 | } |
274 | 0 | case QUERY_NODE_REMOTE_ZERO_ROWS: { |
275 | 0 | SRemoteZeroRowsNode* pRemote = (SRemoteZeroRowsNode*)pNode; |
276 | 0 | *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1); |
277 | 0 | return TSDB_CODE_SUCCESS; |
278 | 0 | } |
279 | 0 | case QUERY_NODE_REMOTE_TABLE: { |
280 | 0 | SRemoteTableNode* pRemote = (SRemoteTableNode*)pNode; |
281 | 0 | *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1); |
282 | 0 | return TSDB_CODE_SUCCESS; |
283 | 0 | } |
284 | 0 | case QUERY_NODE_WHEN_THEN: { |
285 | 0 | SWhenThenNode *pWhenThen = (SWhenThenNode *)pNode; |
286 | 0 | *len += tsnprintf(buf + *len, bufSize - *len, "WHEN "); |
287 | 0 | NODES_ERR_RET(nodesNodeToSQLFormat(pWhenThen->pWhen, buf, bufSize, len, true)); |
288 | 0 | *len += tsnprintf(buf + *len, bufSize - *len, " THEN "); |
289 | 0 | NODES_ERR_RET(nodesNodeToSQLFormat(pWhenThen->pThen, buf, bufSize, len, true)); |
290 | 0 | return TSDB_CODE_SUCCESS; |
291 | 0 | } |
292 | 0 | case QUERY_NODE_CASE_WHEN: { |
293 | 0 | SCaseWhenNode *pCaseWhen = (SCaseWhenNode *)pNode; |
294 | 0 | *len += tsnprintf(buf + *len, bufSize - *len, "CASE"); |
295 | 0 | if (pCaseWhen->pCase) { |
296 | 0 | *len += tsnprintf(buf + *len, bufSize - *len, " "); |
297 | 0 | NODES_ERR_RET(nodesNodeToSQLFormat(pCaseWhen->pCase, buf, bufSize, len, true)); |
298 | 0 | } |
299 | 0 | SNode *pWhenThen = NULL; |
300 | 0 | FOREACH(pWhenThen, pCaseWhen->pWhenThenList) { |
301 | 0 | *len += tsnprintf(buf + *len, bufSize - *len, " "); |
302 | 0 | NODES_ERR_RET(nodesNodeToSQLFormat(pWhenThen, buf, bufSize, len, true)); |
303 | 0 | } |
304 | 0 | if (pCaseWhen->pElse) { |
305 | 0 | *len += tsnprintf(buf + *len, bufSize - *len, " ELSE "); |
306 | 0 | NODES_ERR_RET(nodesNodeToSQLFormat(pCaseWhen->pElse, buf, bufSize, len, true)); |
307 | 0 | } |
308 | 0 | *len += tsnprintf(buf + *len, bufSize - *len, " END"); |
309 | 0 | return TSDB_CODE_SUCCESS; |
310 | 0 | } |
311 | 0 | default: |
312 | 0 | break; |
313 | 0 | } |
314 | | |
315 | 0 | nodesError("nodesNodeToSQL unknown node = %s", nodesNodeName(pNode->type)); |
316 | 0 | NODES_RET(TSDB_CODE_APP_ERROR); |
317 | 0 | } |