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

taosdata / TDengine / #5024

16 Apr 2026 10:31AM UTC coverage: 72.954% (+0.7%) from 72.251%
#5024

push

travis-ci

web-flow
merge: from main to 3.0 branch #35157

120 of 200 new or added lines in 14 files covered. (60.0%)

655 existing lines in 130 files now uncovered.

273150 of 374416 relevant lines covered (72.95%)

129604715.8 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,297,056✔
25
  switch (type) {
149,297,056✔
26
    case OP_TYPE_ADD:
230,487✔
27
      return "+";
230,487✔
28
    case OP_TYPE_SUB:
1,680✔
29
      return "-";
1,680✔
30
    case OP_TYPE_MULTI:
1,680✔
31
      return "*";
1,680✔
32
    case OP_TYPE_DIV:
1,470✔
33
      return "/";
1,470✔
34
    case OP_TYPE_REM:
1,470✔
35
      return "%";
1,470✔
36
    case OP_TYPE_MINUS:
1,890✔
37
      return "-";
1,890✔
38
    case OP_TYPE_BIT_AND:
1,680✔
39
      return "&";
1,680✔
40
    case OP_TYPE_BIT_OR:
1,680✔
41
      return "|";
1,680✔
42
    case OP_TYPE_GREATER_THAN:
17,729,912✔
43
      return ">";
17,729,912✔
44
    case OP_TYPE_GREATER_EQUAL:
28,086,990✔
45
      return ">=";
28,086,990✔
46
    case OP_TYPE_LOWER_THAN:
13,245,299✔
47
      return "<";
13,245,299✔
48
    case OP_TYPE_LOWER_EQUAL:
26,481,202✔
49
      return "<=";
26,481,202✔
50
    case OP_TYPE_EQUAL:
32,001,892✔
51
      return "=";
32,001,892✔
52
    case OP_TYPE_NOT_EQUAL:
1,335,513✔
53
      return "<>";
1,335,513✔
54
    case OP_TYPE_IN:
1,328,616✔
55
      return "IN";
1,328,616✔
56
    case OP_TYPE_NOT_IN:
960,445✔
57
      return "NOT IN";
960,445✔
58
    case OP_TYPE_LIKE:
2,566,194✔
59
      return "LIKE";
2,566,194✔
60
    case OP_TYPE_NOT_LIKE:
840✔
61
      return "NOT LIKE";
840✔
62
    case OP_TYPE_MATCH:
11,304,941✔
63
      return "MATCH";
11,304,941✔
64
    case OP_TYPE_NMATCH:
586,112✔
65
      return "NMATCH";
586,112✔
66
    case OP_TYPE_IS_NULL:
4,046,008✔
67
      return "IS NULL";
4,046,008✔
68
    case OP_TYPE_IS_NOT_NULL:
9,070,789✔
69
      return "IS NOT NULL";
9,070,789✔
70
    case OP_TYPE_IS_TRUE:
305,664✔
71
      return "IS TRUE";
305,664✔
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 "=";
×
92
    default:
4,947✔
93
      break;
4,947✔
94
  }
95
  return "UNKNOWN";
4,947✔
96
}
97

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

111
  return operatorTypeStr(type);
13,218,807✔
112
}
113

114
const char *logicConditionTypeStr(ELogicConditionType type) {
759,626✔
115
  switch (type) {
759,626✔
116
    case LOGIC_COND_TYPE_AND:
497,124✔
117
      return "AND";
497,124✔
118
    case LOGIC_COND_TYPE_OR:
262,502✔
119
      return "OR";
262,502✔
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,264,472✔
129
  return nodesNodeToSQLFormat(pNode, buf, bufSize, len, false);
12,264,472✔
130
}
131

132
int32_t nodesNodeToSQLFormat(SNode *pNode, char *buf, int32_t bufSize, int32_t *len, bool longFormat) {
38,983,243✔
133
  switch (pNode->type) {
38,983,243✔
134
    case QUERY_NODE_COLUMN: {
11,359,902✔
135
      SColumnNode *colNode = (SColumnNode *)pNode;
11,359,902✔
136
      if (colNode->dbName[0]) {
11,359,902✔
137
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->dbName);
10,738,781✔
138
      }
139

140
      if (colNode->tableAlias[0]) {
11,359,902✔
141
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableAlias);
10,758,103✔
142
      } else if (colNode->tableName[0]) {
601,799✔
143
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableName);
×
144
      }
145

146
      if (colNode->node.userAlias[0]) {
11,359,902✔
147
        *len += snprintf(buf + *len, bufSize - *len, "`%s`", colNode->node.userAlias);
11,348,393✔
148
      } else {
149
        *len += snprintf(buf + *len, bufSize - *len, "`%s`", colNode->colName);
11,509✔
150
      }
151

152
      return TSDB_CODE_SUCCESS;
11,359,902✔
153
    }
154
    case QUERY_NODE_VALUE: {
5,291,964✔
155
      SValueNode *colNode = (SValueNode *)pNode;
5,291,964✔
156
      char       *t = nodesGetStrValueFromNode(colNode);
5,291,964✔
157
      if (NULL == t) {
5,292,027✔
158
        // NULL type value (e.g. ELSE NULL in CASE WHEN)
159
        *len += tsnprintf(buf + *len, bufSize - *len, "NULL");
1,096✔
160
        return TSDB_CODE_SUCCESS;
1,096✔
161
      }
162

163
      int32_t tlen = strlen(t);
5,291,025✔
164
      if (longFormat) {
5,291,025✔
165
        *len += snprintf(buf + *len, bufSize - *len, "%s", t);
5,285,777✔
166
      } else {
167
        if (tlen > 32) {
5,248✔
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,248✔
171
        }
172
      }
173

174
      taosMemoryFree(t);
5,291,025✔
175

176
      return TSDB_CODE_SUCCESS;
5,291,025✔
177
    }
178
    case QUERY_NODE_OPERATOR: {
13,317,006✔
179
      SOperatorNode *pOpNode = (SOperatorNode *)pNode;
13,317,006✔
180
      *len += snprintf(buf + *len, bufSize - *len, "(");
13,317,006✔
181
      if (pOpNode->pLeft) {
13,317,006✔
182
        NODES_ERR_RET(nodesNodeToSQLFormat(pOpNode->pLeft, buf, bufSize, len, true));
13,316,802✔
183
      }
184

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

187
      if (pOpNode->pRight) {
13,316,889✔
188
        NODES_ERR_RET(nodesNodeToSQLFormat(pOpNode->pRight, buf, bufSize, len, true));
11,538,119✔
189
      }
190

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

193
      return TSDB_CODE_SUCCESS;
13,317,335✔
194
    }
195
    case QUERY_NODE_LOGIC_CONDITION: {
753,422✔
196
      SLogicConditionNode *pLogicNode = (SLogicConditionNode *)pNode;
753,422✔
197
      SNode               *node = NULL;
753,422✔
198
      bool                 first = true;
753,422✔
199

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

202
      FOREACH(node, pLogicNode->pParameterList) {
2,266,470✔
203
        if (!first) {
1,513,048✔
204
          *len += snprintf(buf + *len, bufSize - *len, " %s ", logicConditionTypeStr(pLogicNode->condType));
759,626✔
205
        }
206
        NODES_ERR_RET(nodesNodeToSQLFormat(node, buf, bufSize, len, true));
1,513,048✔
207
        first = false;
1,513,048✔
208
      }
209

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

212
      return TSDB_CODE_SUCCESS;
753,422✔
213
    }
214
    case QUERY_NODE_FUNCTION: {
202,293✔
215
      SFunctionNode *pFuncNode = (SFunctionNode *)pNode;
202,293✔
216
      SNode         *node = NULL;
202,293✔
217
      bool           first = true;
202,293✔
218

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

221
      FOREACH(node, pFuncNode->pParameterList) {
448,110✔
222
        if (!first) {
245,817✔
223
          *len += snprintf(buf + *len, bufSize - *len, ", ");
43,524✔
224
        }
225
        NODES_ERR_RET(nodesNodeToSQLFormat(node, buf, bufSize, len, true));
245,817✔
226
        first = false;
245,817✔
227
      }
228

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

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

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

241
      FOREACH(node, pListNode->pNodeList) {
34,524✔
242
        if (!first) {
25,886✔
243
          *len += snprintf(buf + *len, bufSize - *len, ", ");
17,248✔
244
          if (!longFormat) {
17,248✔
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));
25,886✔
252
        first = false;
25,886✔
253
      }
254

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

257
      return TSDB_CODE_SUCCESS;
8,638✔
258
    }
259
    case QUERY_NODE_REMOTE_VALUE: {
6,297,683✔
260
      SRemoteValueNode* pRemote = (SRemoteValueNode*)pNode;
6,297,683✔
261
      *len += snprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
6,297,683✔
262
      return TSDB_CODE_SUCCESS;
6,297,683✔
263
    }
264
    case QUERY_NODE_REMOTE_VALUE_LIST: {
1,414,480✔
265
      SRemoteValueListNode* pRemote = (SRemoteValueListNode*)pNode;
1,414,480✔
266
      *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
1,414,480✔
267
      return TSDB_CODE_SUCCESS;
1,414,480✔
268
    }
269
    case QUERY_NODE_REMOTE_ROW: {
200,234✔
270
      SRemoteRowNode* pRemote = (SRemoteRowNode*)pNode;
200,234✔
271
      *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
200,234✔
272
      return TSDB_CODE_SUCCESS;
200,234✔
273
    }
274
    case QUERY_NODE_REMOTE_ZERO_ROWS: {
131,181✔
275
      SRemoteZeroRowsNode* pRemote = (SRemoteZeroRowsNode*)pNode;
131,181✔
276
      *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
131,181✔
277
      return TSDB_CODE_SUCCESS;
131,181✔
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,280✔
285
      SWhenThenNode *pWhenThen = (SWhenThenNode *)pNode;
3,280✔
286
      *len += tsnprintf(buf + *len, bufSize - *len, "WHEN ");
3,280✔
287
      NODES_ERR_RET(nodesNodeToSQLFormat(pWhenThen->pWhen, buf, bufSize, len, true));
3,280✔
288
      *len += tsnprintf(buf + *len, bufSize - *len, " THEN ");
3,280✔
289
      NODES_ERR_RET(nodesNodeToSQLFormat(pWhenThen->pThen, buf, bufSize, len, true));
3,280✔
290
      return TSDB_CODE_SUCCESS;
3,280✔
291
    }
292
    case QUERY_NODE_CASE_WHEN: {
3,280✔
293
      SCaseWhenNode *pCaseWhen = (SCaseWhenNode *)pNode;
3,280✔
294
      *len += tsnprintf(buf + *len, bufSize - *len, "CASE");
3,280✔
295
      if (pCaseWhen->pCase) {
3,280✔
NEW
296
        *len += tsnprintf(buf + *len, bufSize - *len, " ");
×
NEW
297
        NODES_ERR_RET(nodesNodeToSQLFormat(pCaseWhen->pCase, buf, bufSize, len, true));
×
298
      }
299
      SNode *pWhenThen = NULL;
3,280✔
300
      FOREACH(pWhenThen, pCaseWhen->pWhenThenList) {
6,560✔
301
        *len += tsnprintf(buf + *len, bufSize - *len, " ");
3,280✔
302
        NODES_ERR_RET(nodesNodeToSQLFormat(pWhenThen, buf, bufSize, len, true));
3,280✔
303
      }
304
      if (pCaseWhen->pElse) {
3,280✔
305
        *len += tsnprintf(buf + *len, bufSize - *len, " ELSE ");
3,280✔
306
        NODES_ERR_RET(nodesNodeToSQLFormat(pCaseWhen->pElse, buf, bufSize, len, true));
3,280✔
307
      }
308
      *len += tsnprintf(buf + *len, bufSize - *len, " END");
3,280✔
309
      return TSDB_CODE_SUCCESS;
3,280✔
310
    }
UNCOV
311
    default:
×
UNCOV
312
      break;
×
313
  }
314

UNCOV
315
  nodesError("nodesNodeToSQL unknown node = %s", nodesNodeName(pNode->type));
×
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