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

taosdata / TDengine / #4995

18 Mar 2026 06:26AM UTC coverage: 71.996% (-0.2%) from 72.244%
#4995

push

travis-ci

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

2 of 4 new or added lines in 2 files covered. (50.0%)

5312 existing lines in 167 files now uncovered.

244665 of 339830 relevant lines covered (72.0%)

135013613.72 hits per line

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

78.87
/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) {
141,439,781✔
25
  switch (type) {
141,439,781✔
26
    case OP_TYPE_ADD:
212,444✔
27
      return "+";
212,444✔
28
    case OP_TYPE_SUB:
3,136✔
29
      return "-";
3,136✔
30
    case OP_TYPE_MULTI:
3,136✔
31
      return "*";
3,136✔
32
    case OP_TYPE_DIV:
2,744✔
33
      return "/";
2,744✔
34
    case OP_TYPE_REM:
2,744✔
35
      return "%";
2,744✔
36
    case OP_TYPE_MINUS:
3,528✔
37
      return "-";
3,528✔
38
    case OP_TYPE_BIT_AND:
3,136✔
39
      return "&";
3,136✔
40
    case OP_TYPE_BIT_OR:
3,136✔
41
      return "|";
3,136✔
42
    case OP_TYPE_GREATER_THAN:
17,049,301✔
43
      return ">";
17,049,301✔
44
    case OP_TYPE_GREATER_EQUAL:
26,741,101✔
45
      return ">=";
26,741,101✔
46
    case OP_TYPE_LOWER_THAN:
12,746,015✔
47
      return "<";
12,746,015✔
48
    case OP_TYPE_LOWER_EQUAL:
25,421,607✔
49
      return "<=";
25,421,607✔
50
    case OP_TYPE_EQUAL:
29,414,566✔
51
      return "=";
29,414,566✔
52
    case OP_TYPE_NOT_EQUAL:
1,259,550✔
53
      return "<>";
1,259,550✔
54
    case OP_TYPE_IN:
1,205,122✔
55
      return "IN";
1,205,122✔
56
    case OP_TYPE_NOT_IN:
894,815✔
57
      return "NOT IN";
894,815✔
58
    case OP_TYPE_LIKE:
2,366,642✔
59
      return "LIKE";
2,366,642✔
60
    case OP_TYPE_NOT_LIKE:
1,568✔
61
      return "NOT LIKE";
1,568✔
62
    case OP_TYPE_MATCH:
10,878,042✔
63
      return "MATCH";
10,878,042✔
64
    case OP_TYPE_NMATCH:
612,250✔
65
      return "NMATCH";
612,250✔
66
    case OP_TYPE_IS_NULL:
3,842,999✔
67
      return "IS NULL";
3,842,999✔
68
    case OP_TYPE_IS_NOT_NULL:
8,460,983✔
69
      return "IS NOT NULL";
8,460,983✔
70
    case OP_TYPE_IS_TRUE:
311,729✔
71
      return "IS TRUE";
311,729✔
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) {
12,248,237✔
99
  if (isNegative) {
12,248,237✔
100
    switch (type) {
99,681✔
101
      case OP_TYPE_IN:
×
102
        return "!= ANY";
×
103
      case OP_TYPE_NOT_IN:
99,681✔
104
        return "= ALL";
99,681✔
105
      default:
×
106
        break;
×
107
    }
108
    return "UNKNOWN";
×
109
  }
110

111
  return operatorTypeStr(type);
12,148,556✔
112
}
113

114
const char *logicConditionTypeStr(ELogicConditionType type) {
692,130✔
115
  switch (type) {
692,130✔
116
    case LOGIC_COND_TYPE_AND:
452,886✔
117
      return "AND";
452,886✔
118
    case LOGIC_COND_TYPE_OR:
239,335✔
119
      return "OR";
239,335✔
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) {
11,285,100✔
129
  return nodesNodeToSQLFormat(pNode, buf, bufSize, len, false);
11,285,100✔
130
}
131

132
int32_t nodesNodeToSQLFormat(SNode *pNode, char *buf, int32_t bufSize, int32_t *len, bool longFormat) {
35,827,647✔
133
  switch (pNode->type) {
35,827,647✔
134
    case QUERY_NODE_COLUMN: {
10,436,730✔
135
      SColumnNode *colNode = (SColumnNode *)pNode;
10,436,730✔
136
      if (colNode->dbName[0]) {
10,436,730✔
137
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->dbName);
9,891,804✔
138
      }
139

140
      if (colNode->tableAlias[0]) {
10,436,730✔
141
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableAlias);
9,908,819✔
142
      } else if (colNode->tableName[0]) {
527,911✔
143
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableName);
×
144
      }
145

146
      if (colNode->tableAlias[0]) {
10,436,730✔
147
        *len += snprintf(buf + *len, bufSize - *len, "`%s`", colNode->node.userAlias);
9,908,728✔
148
      } else {
149
        *len += snprintf(buf + *len, bufSize - *len, "%s", colNode->node.userAlias);
528,002✔
150
      }
151

152
      return TSDB_CODE_SUCCESS;
10,436,730✔
153
    }
154
    case QUERY_NODE_VALUE: {
4,881,196✔
155
      SValueNode *colNode = (SValueNode *)pNode;
4,881,196✔
156
      char       *t = nodesGetStrValueFromNode(colNode);
4,881,196✔
157
      if (NULL == t) {
4,881,354✔
158
        nodesError("fail to get str value from valueNode");
×
159
        NODES_ERR_RET(TSDB_CODE_APP_ERROR);
×
160
      }
161

162
      int32_t tlen = strlen(t);
4,881,171✔
163
      if (longFormat) {
4,881,171✔
164
        *len += snprintf(buf + *len, bufSize - *len, "%s", t);
4,876,179✔
165
      } else {
166
        if (tlen > 32) {
4,992✔
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);
4,992✔
170
        }
171
      }
172

173
      taosMemoryFree(t);
4,881,171✔
174

175
      return TSDB_CODE_SUCCESS;
4,880,908✔
176
    }
177
    case QUERY_NODE_OPERATOR: {
12,247,651✔
178
      SOperatorNode *pOpNode = (SOperatorNode *)pNode;
12,247,651✔
179
      *len += snprintf(buf + *len, bufSize - *len, "(");
12,247,651✔
180
      if (pOpNode->pLeft) {
12,247,651✔
181
        NODES_ERR_RET(nodesNodeToSQLFormat(pOpNode->pLeft, buf, bufSize, len, true));
12,248,237✔
182
      }
183

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

186
      if (pOpNode->pRight) {
12,248,571✔
187
        NODES_ERR_RET(nodesNodeToSQLFormat(pOpNode->pRight, buf, bufSize, len, true));
10,598,068✔
188
      }
189

190
      *len += snprintf(buf + *len, bufSize - *len, ")");
12,248,557✔
191

192
      return TSDB_CODE_SUCCESS;
12,248,557✔
193
    }
194
    case QUERY_NODE_LOGIC_CONDITION: {
687,393✔
195
      SLogicConditionNode *pLogicNode = (SLogicConditionNode *)pNode;
687,393✔
196
      SNode               *node = NULL;
687,393✔
197
      bool                 first = true;
687,393✔
198

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

201
      FOREACH(node, pLogicNode->pParameterList) {
2,066,916✔
202
        if (!first) {
1,379,523✔
203
          *len += snprintf(buf + *len, bufSize - *len, " %s ", logicConditionTypeStr(pLogicNode->condType));
692,130✔
204
        }
205
        NODES_ERR_RET(nodesNodeToSQLFormat(node, buf, bufSize, len, true));
1,379,523✔
206
        first = false;
1,379,523✔
207
      }
208

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

211
      return TSDB_CODE_SUCCESS;
687,393✔
212
    }
213
    case QUERY_NODE_FUNCTION: {
184,482✔
214
      SFunctionNode *pFuncNode = (SFunctionNode *)pNode;
184,482✔
215
      SNode         *node = NULL;
184,482✔
216
      bool           first = true;
184,482✔
217

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

220
      FOREACH(node, pFuncNode->pParameterList) {
410,436✔
221
        if (!first) {
225,954✔
222
          *len += snprintf(buf + *len, bufSize - *len, ", ");
41,472✔
223
        }
224
        NODES_ERR_RET(nodesNodeToSQLFormat(node, buf, bufSize, len, true));
225,954✔
225
        first = false;
225,954✔
226
      }
227

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

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

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

240
      FOREACH(node, pListNode->pNodeList) {
32,028✔
241
        if (!first) {
24,278✔
242
          *len += snprintf(buf + *len, bufSize - *len, ", ");
16,528✔
243
          if (!longFormat) {
16,528✔
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));
24,278✔
251
        first = false;
24,278✔
252
      }
253

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

256
      return TSDB_CODE_SUCCESS;
7,750✔
257
    }
258
    case QUERY_NODE_REMOTE_VALUE: {
5,753,430✔
259
      SRemoteValueNode* pRemote = (SRemoteValueNode*)pNode;
5,753,430✔
260
      *len += snprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
5,753,430✔
261
      return TSDB_CODE_SUCCESS;
5,753,430✔
262
    }
263
    case QUERY_NODE_REMOTE_VALUE_LIST: {
1,291,339✔
264
      SRemoteValueListNode* pRemote = (SRemoteValueListNode*)pNode;
1,291,339✔
265
      *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
1,291,339✔
266
      return TSDB_CODE_SUCCESS;
1,291,612✔
267
    }
268
    case QUERY_NODE_REMOTE_ROW: {
203,626✔
269
      SRemoteRowNode* pRemote = (SRemoteRowNode*)pNode;
203,626✔
270
      *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
203,626✔
271
      return TSDB_CODE_SUCCESS;
203,626✔
272
    }
273
    case QUERY_NODE_REMOTE_ZERO_ROWS: {
133,493✔
274
      SRemoteZeroRowsNode* pRemote = (SRemoteZeroRowsNode*)pNode;
133,493✔
275
      *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
133,493✔
276
      return TSDB_CODE_SUCCESS;
133,493✔
277
    }
278
    default:
557✔
279
      break;
557✔
280
  }
281

282
  nodesError("nodesNodeToSQL unknown node = %s", nodesNodeName(pNode->type));
557✔
283
  NODES_RET(TSDB_CODE_APP_ERROR);
×
284
}
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