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

taosdata / TDengine / #5021

10 Apr 2026 06:58AM UTC coverage: 72.291% (+0.01%) from 72.28%
#5021

push

travis-ci

web-flow
merge: from main to 3.0 #35103

77 of 116 new or added lines in 3 files covered. (66.38%)

2783 existing lines in 135 files now uncovered.

257546 of 356263 relevant lines covered (72.29%)

131342782.21 hits per line

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

78.79
/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) {
148,271,278✔
25
  switch (type) {
148,271,278✔
26
    case OP_TYPE_ADD:
234,796✔
27
      return "+";
234,796✔
28
    case OP_TYPE_SUB:
1,664✔
29
      return "-";
1,664✔
30
    case OP_TYPE_MULTI:
1,664✔
31
      return "*";
1,664✔
32
    case OP_TYPE_DIV:
1,456✔
33
      return "/";
1,456✔
34
    case OP_TYPE_REM:
1,456✔
35
      return "%";
1,456✔
36
    case OP_TYPE_MINUS:
1,872✔
37
      return "-";
1,872✔
38
    case OP_TYPE_BIT_AND:
1,664✔
39
      return "&";
1,664✔
40
    case OP_TYPE_BIT_OR:
1,664✔
41
      return "|";
1,664✔
42
    case OP_TYPE_GREATER_THAN:
17,735,687✔
43
      return ">";
17,735,687✔
44
    case OP_TYPE_GREATER_EQUAL:
28,038,075✔
45
      return ">=";
28,038,075✔
46
    case OP_TYPE_LOWER_THAN:
13,131,731✔
47
      return "<";
13,131,731✔
48
    case OP_TYPE_LOWER_EQUAL:
26,372,597✔
49
      return "<=";
26,372,597✔
50
    case OP_TYPE_EQUAL:
31,711,589✔
51
      return "=";
31,711,589✔
52
    case OP_TYPE_NOT_EQUAL:
1,350,890✔
53
      return "<>";
1,350,890✔
54
    case OP_TYPE_IN:
1,316,617✔
55
      return "IN";
1,316,617✔
56
    case OP_TYPE_NOT_IN:
971,327✔
57
      return "NOT IN";
971,327✔
58
    case OP_TYPE_LIKE:
2,437,110✔
59
      return "LIKE";
2,437,110✔
60
    case OP_TYPE_NOT_LIKE:
832✔
61
      return "NOT LIKE";
832✔
62
    case OP_TYPE_MATCH:
11,152,250✔
63
      return "MATCH";
11,152,250✔
64
    case OP_TYPE_NMATCH:
604,108✔
65
      return "NMATCH";
604,108✔
66
    case OP_TYPE_IS_NULL:
4,020,666✔
67
      return "IS NULL";
4,020,666✔
68
    case OP_TYPE_IS_NOT_NULL:
8,876,745✔
69
      return "IS NOT NULL";
8,876,745✔
70
    case OP_TYPE_IS_TRUE:
306,461✔
71
      return "IS TRUE";
306,461✔
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:
460✔
93
      break;
460✔
94
  }
95
  return "UNKNOWN";
460✔
96
}
97

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

111
  return operatorTypeStr(type);
13,393,227✔
112
}
113

114
const char *logicConditionTypeStr(ELogicConditionType type) {
750,470✔
115
  switch (type) {
750,470✔
116
    case LOGIC_COND_TYPE_AND:
494,781✔
117
      return "AND";
494,781✔
118
    case LOGIC_COND_TYPE_OR:
255,689✔
119
      return "OR";
255,689✔
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,449,817✔
129
  return nodesNodeToSQLFormat(pNode, buf, bufSize, len, false);
12,449,817✔
130
}
131

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

140
      if (colNode->tableAlias[0]) {
11,484,842✔
141
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableAlias);
10,884,314✔
142
      } else if (colNode->tableName[0]) {
600,528✔
143
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableName);
×
144
      }
145

146
      if (colNode->node.userAlias[0]) {
11,484,842✔
147
        *len += snprintf(buf + *len, bufSize - *len, "`%s`", colNode->node.userAlias);
11,473,908✔
148
      } else {
149
        *len += snprintf(buf + *len, bufSize - *len, "`%s`", colNode->colName);
11,300✔
150
      }
151

152
      return TSDB_CODE_SUCCESS;
11,484,842✔
153
    }
154
    case QUERY_NODE_VALUE: {
5,320,026✔
155
      SValueNode *colNode = (SValueNode *)pNode;
5,320,026✔
156
      char       *t = nodesGetStrValueFromNode(colNode);
5,320,026✔
157
      if (NULL == t) {
5,319,645✔
158
        nodesError("fail to get str value from valueNode");
×
UNCOV
159
        NODES_ERR_RET(TSDB_CODE_APP_ERROR);
×
160
      }
161

162
      int32_t tlen = strlen(t);
5,319,558✔
163
      if (longFormat) {
5,319,558✔
164
        *len += snprintf(buf + *len, bufSize - *len, "%s", t);
5,314,398✔
165
      } else {
166
        if (tlen > 32) {
5,160✔
167
          *len += snprintf(buf + *len, bufSize - *len, "%.*s...%s", 32, t, t + tlen - 1);
×
168
        } else {
169
          *len += snprintf(buf + *len, bufSize - *len, "%s", t);
5,160✔
170
        }
171
      }
172

173
      taosMemoryFree(t);
5,319,558✔
174

175
      return TSDB_CODE_SUCCESS;
5,319,103✔
176
    }
177
    case QUERY_NODE_OPERATOR: {
13,490,803✔
178
      SOperatorNode *pOpNode = (SOperatorNode *)pNode;
13,490,803✔
179
      *len += snprintf(buf + *len, bufSize - *len, "(");
13,490,803✔
180
      if (pOpNode->pLeft) {
13,490,803✔
181
        NODES_ERR_RET(nodesNodeToSQLFormat(pOpNode->pLeft, buf, bufSize, len, true));
13,491,518✔
182
      }
183

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

186
      if (pOpNode->pRight) {
13,491,980✔
187
        NODES_ERR_RET(nodesNodeToSQLFormat(pOpNode->pRight, buf, bufSize, len, true));
11,680,054✔
188
      }
189

190
      *len += snprintf(buf + *len, bufSize - *len, ")");
13,491,950✔
191

192
      return TSDB_CODE_SUCCESS;
13,491,950✔
193
    }
194
    case QUERY_NODE_LOGIC_CONDITION: {
746,750✔
195
      SLogicConditionNode *pLogicNode = (SLogicConditionNode *)pNode;
746,750✔
196
      SNode               *node = NULL;
746,750✔
197
      bool                 first = true;
746,750✔
198

199
      *len += snprintf(buf + *len, bufSize - *len, "(");
746,750✔
200

201
      FOREACH(node, pLogicNode->pParameterList) {
2,244,158✔
202
        if (!first) {
1,497,314✔
203
          *len += snprintf(buf + *len, bufSize - *len, " %s ", logicConditionTypeStr(pLogicNode->condType));
750,470✔
204
        }
205
        NODES_ERR_RET(nodesNodeToSQLFormat(node, buf, bufSize, len, true));
1,497,314✔
206
        first = false;
1,497,408✔
207
      }
208

209
      *len += snprintf(buf + *len, bufSize - *len, ")");
746,844✔
210

211
      return TSDB_CODE_SUCCESS;
746,844✔
212
    }
213
    case QUERY_NODE_FUNCTION: {
206,478✔
214
      SFunctionNode *pFuncNode = (SFunctionNode *)pNode;
206,478✔
215
      SNode         *node = NULL;
206,478✔
216
      bool           first = true;
206,478✔
217

218
      *len += snprintf(buf + *len, bufSize - *len, "%s(", pFuncNode->functionName);
206,478✔
219

220
      FOREACH(node, pFuncNode->pParameterList) {
456,048✔
221
        if (!first) {
249,570✔
222
          *len += snprintf(buf + *len, bufSize - *len, ", ");
43,092✔
223
        }
224
        NODES_ERR_RET(nodesNodeToSQLFormat(node, buf, bufSize, len, true));
249,570✔
225
        first = false;
249,570✔
226
      }
227

228
      *len += snprintf(buf + *len, bufSize - *len, ")");
206,478✔
229

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

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

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

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

256
      return TSDB_CODE_SUCCESS;
8,537✔
257
    }
258
    case QUERY_NODE_REMOTE_VALUE: {
6,427,919✔
259
      SRemoteValueNode* pRemote = (SRemoteValueNode*)pNode;
6,427,919✔
260
      *len += snprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
6,427,919✔
261
      return TSDB_CODE_SUCCESS;
6,427,919✔
262
    }
263
    case QUERY_NODE_REMOTE_VALUE_LIST: {
1,437,552✔
264
      SRemoteValueListNode* pRemote = (SRemoteValueListNode*)pNode;
1,437,552✔
265
      *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
1,437,552✔
266
      return TSDB_CODE_SUCCESS;
1,437,729✔
267
    }
268
    case QUERY_NODE_REMOTE_ROW: {
200,624✔
269
      SRemoteRowNode* pRemote = (SRemoteRowNode*)pNode;
200,624✔
270
      *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
200,624✔
271
      return TSDB_CODE_SUCCESS;
200,624✔
272
    }
273
    case QUERY_NODE_REMOTE_ZERO_ROWS: {
131,552✔
274
      SRemoteZeroRowsNode* pRemote = (SRemoteZeroRowsNode*)pNode;
131,552✔
275
      *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
131,552✔
276
      return TSDB_CODE_SUCCESS;
131,552✔
277
    }
278
    case QUERY_NODE_REMOTE_TABLE: {
×
279
      SRemoteTableNode* pRemote = (SRemoteTableNode*)pNode;
×
280
      *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
×
281
      return TSDB_CODE_SUCCESS;
×
282
    }
283
    default:
1,271✔
284
      break;
1,271✔
285
  }
286

287
  nodesError("nodesNodeToSQL unknown node = %s", nodesNodeName(pNode->type));
1,271✔
288
  NODES_RET(TSDB_CODE_APP_ERROR);
×
289
}
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