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

taosdata / TDengine / #5004

26 Mar 2026 12:51PM UTC coverage: 72.338% (+0.02%) from 72.317%
#5004

push

travis-ci

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

512 of 851 new or added lines in 47 files covered. (60.16%)

5499 existing lines in 143 files now uncovered.

253935 of 351039 relevant lines covered (72.34%)

130889634.0 hits per line

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

77.32
/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) {
145,436,093✔
25
  switch (type) {
145,436,093✔
26
    case OP_TYPE_ADD:
226,696✔
27
      return "+";
226,696✔
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,304,405✔
43
      return ">";
17,304,405✔
44
    case OP_TYPE_GREATER_EQUAL:
27,559,195✔
45
      return ">=";
27,559,195✔
46
    case OP_TYPE_LOWER_THAN:
12,822,279✔
47
      return "<";
12,822,279✔
48
    case OP_TYPE_LOWER_EQUAL:
25,979,007✔
49
      return "<=";
25,979,007✔
50
    case OP_TYPE_EQUAL:
30,907,587✔
51
      return "=";
30,907,587✔
52
    case OP_TYPE_NOT_EQUAL:
1,319,407✔
53
      return "<>";
1,319,407✔
54
    case OP_TYPE_IN:
1,260,898✔
55
      return "IN";
1,260,898✔
56
    case OP_TYPE_NOT_IN:
948,601✔
57
      return "NOT IN";
948,601✔
58
    case OP_TYPE_LIKE:
2,537,232✔
59
      return "LIKE";
2,537,232✔
60
    case OP_TYPE_NOT_LIKE:
832✔
61
      return "NOT LIKE";
832✔
62
    case OP_TYPE_MATCH:
11,041,492✔
63
      return "MATCH";
11,041,492✔
64
    case OP_TYPE_NMATCH:
545,390✔
65
      return "NMATCH";
545,390✔
66
    case OP_TYPE_IS_NULL:
3,961,451✔
67
      return "IS NULL";
3,961,451✔
68
    case OP_TYPE_IS_NOT_NULL:
8,698,647✔
69
      return "IS NOT NULL";
8,698,647✔
70
    case OP_TYPE_IS_TRUE:
312,065✔
71
      return "IS TRUE";
312,065✔
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,159,728✔
99
  if (isNegative) {
13,159,728✔
100
    switch (type) {
99,904✔
101
      case OP_TYPE_IN:
×
102
        return "!= ANY";
×
103
      case OP_TYPE_NOT_IN:
99,904✔
104
        return "= ALL";
99,904✔
105
      default:
×
106
        break;
×
107
    }
108
    return "UNKNOWN";
×
109
  }
110

111
  return operatorTypeStr(type);
13,059,824✔
112
}
113

114
const char *logicConditionTypeStr(ELogicConditionType type) {
761,175✔
115
  switch (type) {
761,175✔
116
    case LOGIC_COND_TYPE_AND:
497,625✔
117
      return "AND";
497,625✔
118
    case LOGIC_COND_TYPE_OR:
263,643✔
119
      return "OR";
263,643✔
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,115,716✔
129
  return nodesNodeToSQLFormat(pNode, buf, bufSize, len, false);
12,115,716✔
130
}
131

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

140
      if (colNode->tableAlias[0]) {
11,219,114✔
141
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableAlias);
10,623,353✔
142
      } else if (colNode->tableName[0]) {
595,761✔
143
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableName);
×
144
      }
145

146
      if (colNode->node.userAlias[0]) {
11,219,114✔
147
        *len += snprintf(buf + *len, bufSize - *len, "`%s`", colNode->node.userAlias);
11,208,306✔
148
      } else {
149
        *len += snprintf(buf + *len, bufSize - *len, "`%s`", colNode->colName);
11,152✔
150
      }
151

152
      return TSDB_CODE_SUCCESS;
11,219,114✔
153
    }
154
    case QUERY_NODE_VALUE: {
5,235,458✔
155
      SValueNode *colNode = (SValueNode *)pNode;
5,235,458✔
156
      char       *t = nodesGetStrValueFromNode(colNode);
5,235,458✔
157
      if (NULL == t) {
5,235,272✔
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);
5,235,272✔
163
      if (longFormat) {
5,235,272✔
164
        *len += snprintf(buf + *len, bufSize - *len, "%s", t);
5,230,152✔
165
      } else {
166
        if (tlen > 32) {
5,120✔
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,120✔
170
        }
171
      }
172

173
      taosMemoryFree(t);
5,235,272✔
174

175
      return TSDB_CODE_SUCCESS;
5,235,231✔
176
    }
177
    case QUERY_NODE_OPERATOR: {
13,159,901✔
178
      SOperatorNode *pOpNode = (SOperatorNode *)pNode;
13,159,901✔
179
      *len += snprintf(buf + *len, bufSize - *len, "(");
13,159,901✔
180
      if (pOpNode->pLeft) {
13,159,901✔
181
        NODES_ERR_RET(nodesNodeToSQLFormat(pOpNode->pLeft, buf, bufSize, len, true));
13,159,998✔
182
      }
183

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

186
      if (pOpNode->pRight) {
13,160,165✔
187
        NODES_ERR_RET(nodesNodeToSQLFormat(pOpNode->pRight, buf, bufSize, len, true));
11,398,863✔
188
      }
189

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

192
      return TSDB_CODE_SUCCESS;
13,159,821✔
193
    }
194
    case QUERY_NODE_LOGIC_CONDITION: {
749,658✔
195
      SLogicConditionNode *pLogicNode = (SLogicConditionNode *)pNode;
749,658✔
196
      SNode               *node = NULL;
749,658✔
197
      bool                 first = true;
749,658✔
198

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

201
      FOREACH(node, pLogicNode->pParameterList) {
2,260,584✔
202
        if (!first) {
1,511,019✔
203
          *len += snprintf(buf + *len, bufSize - *len, " %s ", logicConditionTypeStr(pLogicNode->condType));
761,175✔
204
        }
205
        NODES_ERR_RET(nodesNodeToSQLFormat(node, buf, bufSize, len, true));
1,511,112✔
206
        first = false;
1,510,926✔
207
      }
208

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

211
      return TSDB_CODE_SUCCESS;
749,565✔
212
    }
213
    case QUERY_NODE_FUNCTION: {
199,005✔
214
      SFunctionNode *pFuncNode = (SFunctionNode *)pNode;
199,005✔
215
      SNode         *node = NULL;
199,005✔
216
      bool           first = true;
199,005✔
217

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

220
      FOREACH(node, pFuncNode->pParameterList) {
440,454✔
221
        if (!first) {
241,449✔
222
          *len += snprintf(buf + *len, bufSize - *len, ", ");
42,444✔
223
        }
224
        NODES_ERR_RET(nodesNodeToSQLFormat(node, buf, bufSize, len, true));
241,449✔
225
        first = false;
241,449✔
226
      }
227

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

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

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

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

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

256
      return TSDB_CODE_SUCCESS;
8,467✔
257
    }
258
    case QUERY_NODE_REMOTE_VALUE: {
6,208,992✔
259
      SRemoteValueNode* pRemote = (SRemoteValueNode*)pNode;
6,208,992✔
260
      *len += snprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
6,208,992✔
261
      return TSDB_CODE_SUCCESS;
6,208,992✔
262
    }
263
    case QUERY_NODE_REMOTE_VALUE_LIST: {
1,396,372✔
264
      SRemoteValueListNode* pRemote = (SRemoteValueListNode*)pNode;
1,396,372✔
265
      *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
1,396,372✔
266
      return TSDB_CODE_SUCCESS;
1,396,279✔
267
    }
268
    case QUERY_NODE_REMOTE_ROW: {
204,152✔
269
      SRemoteRowNode* pRemote = (SRemoteRowNode*)pNode;
204,152✔
270
      *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
204,152✔
271
      return TSDB_CODE_SUCCESS;
204,152✔
272
    }
273
    case QUERY_NODE_REMOTE_ZERO_ROWS: {
133,844✔
274
      SRemoteZeroRowsNode* pRemote = (SRemoteZeroRowsNode*)pNode;
133,844✔
275
      *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
133,844✔
276
      return TSDB_CODE_SUCCESS;
133,844✔
277
    }
278
    default:
×
279
      break;
×
280
  }
281

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