• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #5026

17 Apr 2026 01:15AM UTC coverage: 72.966% (-0.04%) from 73.009%
#5026

push

travis-ci

web-flow
fix: replace \s with [[:space:]] in shell regex for macOS BSD compat (#35162)

6 of 7 new or added lines in 2 files covered. (85.71%)

605 existing lines in 129 files now uncovered.

273168 of 374375 relevant lines covered (72.97%)

129569613.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.55
/source/libs/nodes/src/nodesToSQLFuncs.c
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
const char *operatorTypeStr(EOperatorType type) {
149,868,282✔
25
  switch (type) {
149,868,282✔
26
    case OP_TYPE_ADD:
238,645✔
27
      return "+";
238,645✔
28
    case OP_TYPE_SUB:
3,232✔
29
      return "-";
3,232✔
30
    case OP_TYPE_MULTI:
3,232✔
31
      return "*";
3,232✔
32
    case OP_TYPE_DIV:
2,828✔
33
      return "/";
2,828✔
34
    case OP_TYPE_REM:
2,828✔
35
      return "%";
2,828✔
36
    case OP_TYPE_MINUS:
3,636✔
37
      return "-";
3,636✔
38
    case OP_TYPE_BIT_AND:
3,232✔
39
      return "&";
3,232✔
40
    case OP_TYPE_BIT_OR:
3,232✔
41
      return "|";
3,232✔
42
    case OP_TYPE_GREATER_THAN:
17,865,288✔
43
      return ">";
17,865,288✔
44
    case OP_TYPE_GREATER_EQUAL:
28,098,740✔
45
      return ">=";
28,098,740✔
46
    case OP_TYPE_LOWER_THAN:
13,238,459✔
47
      return "<";
13,238,459✔
48
    case OP_TYPE_LOWER_EQUAL:
26,554,927✔
49
      return "<=";
26,554,927✔
50
    case OP_TYPE_EQUAL:
32,275,650✔
51
      return "=";
32,275,650✔
52
    case OP_TYPE_NOT_EQUAL:
1,368,382✔
53
      return "<>";
1,368,382✔
54
    case OP_TYPE_IN:
1,344,796✔
55
      return "IN";
1,344,796✔
56
    case OP_TYPE_NOT_IN:
984,615✔
57
      return "NOT IN";
984,615✔
58
    case OP_TYPE_LIKE:
2,445,505✔
59
      return "LIKE";
2,445,505✔
60
    case OP_TYPE_NOT_LIKE:
1,616✔
61
      return "NOT LIKE";
1,616✔
62
    case OP_TYPE_MATCH:
11,343,332✔
63
      return "MATCH";
11,343,332✔
64
    case OP_TYPE_NMATCH:
494,934✔
65
      return "NMATCH";
494,934✔
66
    case OP_TYPE_IS_NULL:
4,073,982✔
67
      return "IS NULL";
4,073,982✔
68
    case OP_TYPE_IS_NOT_NULL:
9,208,881✔
69
      return "IS NOT NULL";
9,208,881✔
70
    case OP_TYPE_IS_TRUE:
311,335✔
71
      return "IS TRUE";
311,335✔
72
    case OP_TYPE_IS_FALSE:
×
73
      return "IS FALSE";
×
74
    case OP_TYPE_IS_UNKNOWN:
×
75
      return "IS UNKNOWN";
×
76
    case OP_TYPE_IS_NOT_TRUE:
×
77
      return "IS NOT TRUE";
×
78
    case OP_TYPE_IS_NOT_FALSE:
×
79
      return "IS NOT FALSE";
×
80
    case OP_TYPE_IS_NOT_UNKNOWN:
×
81
      return "IS NOT UNKNOWN";
×
82
    case OP_TYPE_EXISTS:
×
83
      return "EXISTS";
×
84
    case OP_TYPE_NOT_EXISTS:
×
85
      return "NOT EXISTS";
×
86
    case OP_TYPE_JSON_GET_VALUE:
×
87
      return "=>";
×
88
    case OP_TYPE_JSON_CONTAINS:
×
89
      return "CONTAINS";
×
90
    case OP_TYPE_ASSIGN:
×
91
      return "=";
×
UNCOV
92
    default:
×
UNCOV
93
      break;
×
94
  }
UNCOV
95
  return "UNKNOWN";
×
96
}
97

98
const char *operatorTypeStrEx(EOperatorType type, bool isNegative) {
13,658,601✔
99
  if (isNegative) {
13,658,601✔
100
    switch (type) {
99,458✔
101
      case OP_TYPE_IN:
×
102
        return "!= ANY";
×
103
      case OP_TYPE_NOT_IN:
99,458✔
104
        return "= ALL";
99,458✔
105
      default:
×
106
        break;
×
107
    }
108
    return "UNKNOWN";
×
109
  }
110

111
  return operatorTypeStr(type);
13,559,143✔
112
}
113

114
const char *logicConditionTypeStr(ELogicConditionType type) {
768,433✔
115
  switch (type) {
768,433✔
116
    case LOGIC_COND_TYPE_AND:
505,788✔
117
      return "AND";
505,788✔
118
    case LOGIC_COND_TYPE_OR:
262,645✔
119
      return "OR";
262,645✔
120
    case LOGIC_COND_TYPE_NOT:
×
121
      return "NOT";
×
122
    default:
×
123
      break;
×
124
  }
125
  return "UNKNOWN";
×
126
}
127

128
int32_t nodesNodeToSQL(SNode *pNode, char *buf, int32_t bufSize, int32_t *len) {
12,596,995✔
129
  return nodesNodeToSQLFormat(pNode, buf, bufSize, len, false);
12,596,995✔
130
}
131

132
int32_t nodesNodeToSQLFormat(SNode *pNode, char *buf, int32_t bufSize, int32_t *len, bool longFormat) {
39,967,357✔
133
  switch (pNode->type) {
39,967,357✔
134
    case QUERY_NODE_COLUMN: {
11,633,749✔
135
      SColumnNode *colNode = (SColumnNode *)pNode;
11,633,749✔
136
      if (colNode->dbName[0]) {
11,633,749✔
137
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->dbName);
10,997,361✔
138
      }
139

140
      if (colNode->tableAlias[0]) {
11,633,749✔
141
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableAlias);
11,016,816✔
142
      } else if (colNode->tableName[0]) {
616,933✔
143
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableName);
×
144
      }
145

146
      if (colNode->node.userAlias[0]) {
11,633,749✔
147
        *len += snprintf(buf + *len, bufSize - *len, "`%s`", colNode->node.userAlias);
11,621,446✔
148
      } else {
149
        *len += snprintf(buf + *len, bufSize - *len, "`%s`", colNode->colName);
12,411✔
150
      }
151

152
      return TSDB_CODE_SUCCESS;
11,633,749✔
153
    }
154
    case QUERY_NODE_VALUE: {
5,402,834✔
155
      SValueNode *colNode = (SValueNode *)pNode;
5,402,834✔
156
      char       *t = nodesGetStrValueFromNode(colNode);
5,402,834✔
157
      if (NULL == t) {
5,402,910✔
158
        // NULL type value (e.g. ELSE NULL in CASE WHEN)
159
        *len += tsnprintf(buf + *len, bufSize - *len, "NULL");
1,181✔
160
        return TSDB_CODE_SUCCESS;
1,098✔
161
      }
162

163
      int32_t tlen = strlen(t);
5,401,733✔
164
      if (longFormat) {
5,401,733✔
165
        *len += snprintf(buf + *len, bufSize - *len, "%s", t);
5,396,477✔
166
      } else {
167
        if (tlen > 32) {
5,256✔
168
          *len += snprintf(buf + *len, bufSize - *len, "%.*s...%s", 32, t, t + tlen - 1);
×
169
        } else {
170
          *len += snprintf(buf + *len, bufSize - *len, "%s", t);
5,256✔
171
        }
172
      }
173

174
      taosMemoryFree(t);
5,401,733✔
175

176
      return TSDB_CODE_SUCCESS;
5,401,742✔
177
    }
178
    case QUERY_NODE_OPERATOR: {
13,657,940✔
179
      SOperatorNode *pOpNode = (SOperatorNode *)pNode;
13,657,940✔
180
      *len += snprintf(buf + *len, bufSize - *len, "(");
13,657,940✔
181
      if (pOpNode->pLeft) {
13,657,940✔
182
        NODES_ERR_RET(nodesNodeToSQLFormat(pOpNode->pLeft, buf, bufSize, len, true));
13,658,865✔
183
      }
184

185
      *len += tsnprintf(buf + *len, bufSize - *len, " %s ", operatorTypeStrEx(pOpNode->opType, pOpNode->flag & OPERATOR_FLAG_NEGATIVE_OP));
13,657,695✔
186

187
      if (pOpNode->pRight) {
13,659,328✔
188
        NODES_ERR_RET(nodesNodeToSQLFormat(pOpNode->pRight, buf, bufSize, len, true));
11,828,599✔
189
      }
190

191
      *len += snprintf(buf + *len, bufSize - *len, ")");
13,658,679✔
192

193
      return TSDB_CODE_SUCCESS;
13,658,679✔
194
    }
195
    case QUERY_NODE_LOGIC_CONDITION: {
763,558✔
196
      SLogicConditionNode *pLogicNode = (SLogicConditionNode *)pNode;
763,558✔
197
      SNode               *node = NULL;
763,558✔
198
      bool                 first = true;
763,558✔
199

200
      *len += snprintf(buf + *len, bufSize - *len, "(");
763,558✔
201

202
      FOREACH(node, pLogicNode->pParameterList) {
2,295,549✔
203
        if (!first) {
1,531,991✔
204
          *len += snprintf(buf + *len, bufSize - *len, " %s ", logicConditionTypeStr(pLogicNode->condType));
768,433✔
205
        }
206
        NODES_ERR_RET(nodesNodeToSQLFormat(node, buf, bufSize, len, true));
1,531,991✔
207
        first = false;
1,531,991✔
208
      }
209

210
      *len += snprintf(buf + *len, bufSize - *len, ")");
763,558✔
211

212
      return TSDB_CODE_SUCCESS;
763,558✔
213
    }
214
    case QUERY_NODE_FUNCTION: {
208,342✔
215
      SFunctionNode *pFuncNode = (SFunctionNode *)pNode;
208,342✔
216
      SNode         *node = NULL;
208,342✔
217
      bool           first = true;
208,342✔
218

219
      *len += snprintf(buf + *len, bufSize - *len, "%s(", pFuncNode->functionName);
208,342✔
220

221
      FOREACH(node, pFuncNode->pParameterList) {
460,532✔
222
        if (!first) {
252,273✔
223
          *len += snprintf(buf + *len, bufSize - *len, ", ");
43,848✔
224
        }
225
        NODES_ERR_RET(nodesNodeToSQLFormat(node, buf, bufSize, len, true));
252,273✔
226
        first = false;
252,190✔
227
      }
228

229
      *len += snprintf(buf + *len, bufSize - *len, ")");
208,259✔
230

231
      return TSDB_CODE_SUCCESS;
208,259✔
232
    }
233
    case QUERY_NODE_NODE_LIST: {
8,684✔
234
      SNodeListNode *pListNode = (SNodeListNode *)pNode;
8,684✔
235
      SNode         *node = NULL;
8,684✔
236
      bool           first = true;
8,684✔
237
      int32_t        num = 0;
8,684✔
238

239
      *len += snprintf(buf + *len, bufSize - *len, "(");
8,684✔
240

241
      FOREACH(node, pListNode->pNodeList) {
34,712✔
242
        if (!first) {
26,028✔
243
          *len += snprintf(buf + *len, bufSize - *len, ", ");
17,344✔
244
          if (!longFormat) {
17,344✔
245
            if (++num >= 10) {
×
246
              *len += snprintf(buf + *len, bufSize - *len, "...");
×
247
              break;
×
248
            }
249
          }
250
        }
251
        NODES_ERR_RET(nodesNodeToSQLFormat(node, buf, bufSize, len, true));
26,028✔
252
        first = false;
26,028✔
253
      }
254

255
      *len += snprintf(buf + *len, bufSize - *len, ")");
8,684✔
256

257
      return TSDB_CODE_SUCCESS;
8,684✔
258
    }
259
    case QUERY_NODE_REMOTE_VALUE: {
6,491,208✔
260
      SRemoteValueNode* pRemote = (SRemoteValueNode*)pNode;
6,491,208✔
261
      *len += snprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
6,491,208✔
262
      return TSDB_CODE_SUCCESS;
6,491,208✔
263
    }
264
    case QUERY_NODE_REMOTE_VALUE_LIST: {
1,457,376✔
265
      SRemoteValueListNode* pRemote = (SRemoteValueListNode*)pNode;
1,457,376✔
266
      *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
1,457,376✔
267
      return TSDB_CODE_SUCCESS;
1,457,188✔
268
    }
269
    case QUERY_NODE_REMOTE_ROW: {
203,178✔
270
      SRemoteRowNode* pRemote = (SRemoteRowNode*)pNode;
203,178✔
271
      *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
203,178✔
272
      return TSDB_CODE_SUCCESS;
203,178✔
273
    }
274
    case QUERY_NODE_REMOTE_ZERO_ROWS: {
133,270✔
275
      SRemoteZeroRowsNode* pRemote = (SRemoteZeroRowsNode*)pNode;
133,270✔
276
      *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
133,270✔
277
      return TSDB_CODE_SUCCESS;
133,270✔
278
    }
279
    case QUERY_NODE_REMOTE_TABLE: {
×
280
      SRemoteTableNode* pRemote = (SRemoteTableNode*)pNode;
×
281
      *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
×
282
      return TSDB_CODE_SUCCESS;
×
283
    }
284
    case QUERY_NODE_WHEN_THEN: {
3,290✔
285
      SWhenThenNode *pWhenThen = (SWhenThenNode *)pNode;
3,290✔
286
      *len += tsnprintf(buf + *len, bufSize - *len, "WHEN ");
3,290✔
287
      NODES_ERR_RET(nodesNodeToSQLFormat(pWhenThen->pWhen, buf, bufSize, len, true));
3,290✔
288
      *len += tsnprintf(buf + *len, bufSize - *len, " THEN ");
3,290✔
289
      NODES_ERR_RET(nodesNodeToSQLFormat(pWhenThen->pThen, buf, bufSize, len, true));
3,290✔
290
      return TSDB_CODE_SUCCESS;
3,290✔
291
    }
292
    case QUERY_NODE_CASE_WHEN: {
3,290✔
293
      SCaseWhenNode *pCaseWhen = (SCaseWhenNode *)pNode;
3,290✔
294
      *len += tsnprintf(buf + *len, bufSize - *len, "CASE");
3,290✔
295
      if (pCaseWhen->pCase) {
3,290✔
296
        *len += tsnprintf(buf + *len, bufSize - *len, " ");
×
297
        NODES_ERR_RET(nodesNodeToSQLFormat(pCaseWhen->pCase, buf, bufSize, len, true));
×
298
      }
299
      SNode *pWhenThen = NULL;
3,290✔
300
      FOREACH(pWhenThen, pCaseWhen->pWhenThenList) {
6,580✔
301
        *len += tsnprintf(buf + *len, bufSize - *len, " ");
3,290✔
302
        NODES_ERR_RET(nodesNodeToSQLFormat(pWhenThen, buf, bufSize, len, true));
3,290✔
303
      }
304
      if (pCaseWhen->pElse) {
3,290✔
305
        *len += tsnprintf(buf + *len, bufSize - *len, " ELSE ");
3,290✔
306
        NODES_ERR_RET(nodesNodeToSQLFormat(pCaseWhen->pElse, buf, bufSize, len, true));
3,290✔
307
      }
308
      *len += tsnprintf(buf + *len, bufSize - *len, " END");
3,290✔
309
      return TSDB_CODE_SUCCESS;
3,290✔
310
    }
311
    default:
649✔
312
      break;
649✔
313
  }
314

315
  nodesError("nodesNodeToSQL unknown node = %s", nodesNodeName(pNode->type));
649✔
316
  NODES_RET(TSDB_CODE_APP_ERROR);
×
317
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc