parSelectTest.cpp 8.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "parTestUtil.h"

using namespace std;

namespace ParserTest {

class ParserSelectTest : public ParserTestBase {};

TEST_F(ParserSelectTest, basic) {
  useDb("root", "test");

X
Xiaoyu Wang 已提交
27
  run("SELECT * FROM t1");
28

X
Xiaoyu Wang 已提交
29
  run("SELECT * FROM test.t1");
30

X
Xiaoyu Wang 已提交
31
  run("SELECT ts, c1 FROM t1");
32

X
Xiaoyu Wang 已提交
33
  run("SELECT ts, t.c1 FROM (SELECT * FROM t1) t");
34

X
Xiaoyu Wang 已提交
35
  run("SELECT * FROM t1 tt1, t1 tt2 WHERE tt1.c1 = tt2.c1");
36 37 38 39 40
}

TEST_F(ParserSelectTest, constant) {
  useDb("root", "test");

X
Xiaoyu Wang 已提交
41
  run("SELECT 123, 20.4, 'abc', \"wxy\", timestamp '2022-02-09 17:30:20', true, false, 10s FROM t1");
42

X
Xiaoyu Wang 已提交
43 44
  run("SELECT 1234567890123456789012345678901234567890, 20.1234567890123456789012345678901234567890, 'abc', \"wxy\", "
      "timestamp '2022-02-09 17:30:20', true, false, 15s FROM t1");
45

X
Xiaoyu Wang 已提交
46
  run("SELECT 123 + 45 FROM t1 WHERE 2 - 1");
47 48 49 50 51
}

TEST_F(ParserSelectTest, expression) {
  useDb("root", "test");

X
Xiaoyu Wang 已提交
52
  run("SELECT ts + 10s, c1 + 10, concat(c2, 'abc') FROM t1");
53

X
Xiaoyu Wang 已提交
54
  run("SELECT ts > 0, c1 < 20 and c2 = 'qaz' FROM t1");
55

X
Xiaoyu Wang 已提交
56
  run("SELECT ts > 0, c1 between 10 and 20 and c2 = 'qaz' FROM t1");
57 58 59 60 61
}

TEST_F(ParserSelectTest, condition) {
  useDb("root", "test");

X
Xiaoyu Wang 已提交
62
  run("SELECT c1 FROM t1 WHERE ts in (true, false)");
63

X
Xiaoyu Wang 已提交
64
  run("SELECT * FROM t1 WHERE c1 > 10 and c1 is not null");
65 66 67 68 69
}

TEST_F(ParserSelectTest, pseudoColumn) {
  useDb("root", "test");

70
  run("SELECT _WSTARTTS, _WENDTS, COUNT(*) FROM t1 INTERVAL(10s)");
71 72 73 74 75
}

TEST_F(ParserSelectTest, multiResFunc) {
  useDb("root", "test");

76
  run("SELECT LAST(*), FIRST(*), LAST_ROW(*) FROM t1");
77

78
  run("SELECT LAST(c1, c2), FIRST(t1.*), LAST_ROW(c3) FROM t1");
79

80
  run("SELECT LAST(t2.*), FIRST(t1.c1, t2.*), LAST_ROW(t1.*, t2.*) FROM st1s1 t1, st1s2 t2 WHERE t1.ts = t2.ts");
81 82
}

X
Xiaoyu Wang 已提交
83 84 85
TEST_F(ParserSelectTest, timelineFunc) {
  useDb("root", "test");

86
  run("SELECT LAST(*), FIRST(*) FROM t1");
X
Xiaoyu Wang 已提交
87

88
  run("SELECT FIRST(ts), FIRST(c1), FIRST(c2), FIRST(c3) FROM t1");
X
Xiaoyu Wang 已提交
89

90 91 92
  run("SELECT LAST(*), FIRST(*) FROM t1 GROUP BY c1");

  run("SELECT LAST(*), FIRST(*) FROM t1 INTERVAL(10s)");
X
Xiaoyu Wang 已提交
93

X
Xiaoyu Wang 已提交
94
  run("SELECT diff(c1) FROM t1");
X
Xiaoyu Wang 已提交
95 96
}

97 98
TEST_F(ParserSelectTest, selectFunc) {
  useDb("root", "test");
99

100 101 102 103 104 105 106 107 108
  // select function
  run("SELECT MAX(c1), MIN(c1) FROM t1");
  // select function for GROUP BY clause
  run("SELECT MAX(c1), MIN(c1) FROM t1 GROUP BY c1");
  // select function for INTERVAL clause
  run("SELECT MAX(c1), MIN(c1) FROM t1 INTERVAL(10s)");
  // select function along with the columns of select row
  run("SELECT MAX(c1), c2 FROM t1");
  run("SELECT MAX(c1), t1.* FROM t1");
109 110 111 112 113 114 115
  // select function along with the columns of select row, and with GROUP BY clause
  run("SELECT MAX(c1), c2 FROM t1 GROUP BY c3");
  run("SELECT MAX(c1), t1.* FROM t1 GROUP BY c3");
  // select function along with the columns of select row, and with window clause
  run("SELECT MAX(c1), c2 FROM t1 INTERVAL(10s)");
  run("SELECT MAX(c1), c2 FROM t1 SESSION(ts, 10s)");
  run("SELECT MAX(c1), c2 FROM t1 STATE_WINDOW(c3)");
116 117
}

118 119 120
TEST_F(ParserSelectTest, clause) {
  useDb("root", "test");

121
  // GROUP BY clause
X
Xiaoyu Wang 已提交
122
  run("SELECT COUNT(*) cnt FROM t1 WHERE c1 > 0");
123

124
  run("SELECT COUNT(*), c2 cnt FROM t1 WHERE c1 > 0 GROUP BY c2");
125

126
  run("SELECT COUNT(*) cnt FROM t1 WHERE c1 > 0 GROUP BY c2 having COUNT(c1) > 10");
127

128
  run("SELECT COUNT(*), c1, c2 + 10, c1 + c2 cnt FROM t1 WHERE c1 > 0 GROUP BY c2, c1");
129

130
  run("SELECT COUNT(*), c1 + 10, c2 cnt FROM t1 WHERE c1 > 0 GROUP BY c1 + 10, c2");
131 132

  // order by clause
133
  run("SELECT COUNT(*) cnt FROM t1 WHERE c1 > 0 GROUP BY c2 order by cnt");
134

135
  run("SELECT COUNT(*) cnt FROM t1 WHERE c1 > 0 GROUP BY c2 order by 1");
136 137

  // distinct clause
X
Xiaoyu Wang 已提交
138
  // run("SELECT distinct c1, c2 FROM t1 WHERE c1 > 0 order by c1");
139

X
Xiaoyu Wang 已提交
140
  // run("SELECT distinct c1 + 10, c2 FROM t1 WHERE c1 > 0 order by c1 + 10, c2");
141

X
Xiaoyu Wang 已提交
142
  // run("SELECT distinct c1 + 10 cc1, c2 cc2 FROM t1 WHERE c1 > 0 order by cc1, c2");
143

144
  // run("SELECT distinct COUNT(c2) FROM t1 WHERE c1 > 0 GROUP BY c1 order by COUNT(c2)");
145 146
}

X
Xiaoyu Wang 已提交
147 148 149 150
// INTERVAL(interval_val [, interval_offset]) [SLIDING (sliding_val)] [FILL(fill_mod_and_val)]
// fill_mod_and_val = { NONE | PREV | NULL | LINEAR | NEXT | value_mod }
// value_mod = VALUE , val ...
TEST_F(ParserSelectTest, interval) {
151
  useDb("root", "test");
X
Xiaoyu Wang 已提交
152 153 154 155 156 157 158
  // INTERVAL(interval_val)
  run("SELECT COUNT(*) FROM t1 INTERVAL(10s)");
  // INTERVAL(interval_val, interval_offset)
  run("SELECT COUNT(*) FROM t1 INTERVAL(10s, 5s)");
  // INTERVAL(interval_val, interval_offset) SLIDING (sliding_val)
  run("SELECT COUNT(*) FROM t1 INTERVAL(10s, 5s) SLIDING(7s)");
  // INTERVAL(interval_val) FILL(NONE)
X
Xiaoyu Wang 已提交
159 160
  run("SELECT COUNT(*) FROM t1 WHERE ts > TIMESTAMP '2022-04-01 00:00:00' and ts < TIMESTAMP '2022-04-30 23:59:59' "
      "INTERVAL(10s) FILL(NONE)");
161 162
}

163 164 165
TEST_F(ParserSelectTest, intervalSemanticCheck) {
  useDb("root", "test");

166
  run("SELECT c1 FROM t1 INTERVAL(10s)", TSDB_CODE_PAR_NOT_SINGLE_GROUP, PARSER_STAGE_TRANSLATE);
167 168
  run("SELECT DISTINCT c1, c2 FROM t1 WHERE c1 > 3 INTERVAL(1d) FILL(NEXT)", TSDB_CODE_PAR_INVALID_FILL_TIME_RANGE,
      PARSER_STAGE_TRANSLATE);
169 170
}

171 172 173 174
TEST_F(ParserSelectTest, semanticError) {
  useDb("root", "test");

  // TSDB_CODE_PAR_INVALID_COLUMN
X
Xiaoyu Wang 已提交
175
  run("SELECT c1, cc1 FROM t1", TSDB_CODE_PAR_INVALID_COLUMN, PARSER_STAGE_TRANSLATE);
176

X
Xiaoyu Wang 已提交
177
  run("SELECT t1.c1, t1.cc1 FROM t1", TSDB_CODE_PAR_INVALID_COLUMN, PARSER_STAGE_TRANSLATE);
178 179

  // TSDB_CODE_PAR_TABLE_NOT_EXIST
X
Xiaoyu Wang 已提交
180
  run("SELECT * FROM t10", TSDB_CODE_PAR_TABLE_NOT_EXIST, PARSER_STAGE_TRANSLATE);
181

X
Xiaoyu Wang 已提交
182
  run("SELECT * FROM test.t10", TSDB_CODE_PAR_TABLE_NOT_EXIST, PARSER_STAGE_TRANSLATE);
183

X
Xiaoyu Wang 已提交
184
  run("SELECT t2.c1 FROM t1", TSDB_CODE_PAR_TABLE_NOT_EXIST, PARSER_STAGE_TRANSLATE);
185 186

  // TSDB_CODE_PAR_AMBIGUOUS_COLUMN
X
Xiaoyu Wang 已提交
187
  run("SELECT c2 FROM t1 tt1, t1 tt2 WHERE tt1.c1 = tt2.c1", TSDB_CODE_PAR_AMBIGUOUS_COLUMN, PARSER_STAGE_TRANSLATE);
188 189

  // TSDB_CODE_PAR_WRONG_VALUE_TYPE
190
  run("SELECT timestamp '2010a' FROM t1", TSDB_CODE_PAR_WRONG_VALUE_TYPE, PARSER_STAGE_TRANSLATE);
191 192

  // TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION
X
Xiaoyu Wang 已提交
193
  run("SELECT c2 FROM t1 tt1 join t1 tt2 on COUNT(*) > 0", TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION,
194 195
      PARSER_STAGE_TRANSLATE);

X
Xiaoyu Wang 已提交
196
  run("SELECT c2 FROM t1 WHERE COUNT(*) > 0", TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION, PARSER_STAGE_TRANSLATE);
197

198
  run("SELECT c2 FROM t1 GROUP BY COUNT(*)", TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION, PARSER_STAGE_TRANSLATE);
199 200

  // TSDB_CODE_PAR_WRONG_NUMBER_OF_SELECT
X
Xiaoyu Wang 已提交
201
  run("SELECT c2 FROM t1 order by 0", TSDB_CODE_PAR_WRONG_NUMBER_OF_SELECT, PARSER_STAGE_TRANSLATE);
202

X
Xiaoyu Wang 已提交
203
  run("SELECT c2 FROM t1 order by 2", TSDB_CODE_PAR_WRONG_NUMBER_OF_SELECT, PARSER_STAGE_TRANSLATE);
204 205

  // TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION
X
Xiaoyu Wang 已提交
206
  run("SELECT COUNT(*) cnt FROM t1 having c1 > 0", TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION, PARSER_STAGE_TRANSLATE);
207

208
  run("SELECT COUNT(*) cnt FROM t1 GROUP BY c2 having c1 > 0", TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION,
209 210
      PARSER_STAGE_TRANSLATE);

211
  run("SELECT COUNT(*), c1 cnt FROM t1 GROUP BY c2 having c2 > 0", TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION,
212 213
      PARSER_STAGE_TRANSLATE);

214
  run("SELECT COUNT(*) cnt FROM t1 GROUP BY c2 having c2 > 0 order by c1", TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION,
215 216 217
      PARSER_STAGE_TRANSLATE);

  // TSDB_CODE_PAR_NOT_SINGLE_GROUP
X
Xiaoyu Wang 已提交
218
  run("SELECT COUNT(*), c1 FROM t1", TSDB_CODE_PAR_NOT_SINGLE_GROUP, PARSER_STAGE_TRANSLATE);
219

X
Xiaoyu Wang 已提交
220
  run("SELECT COUNT(*) FROM t1 order by c1", TSDB_CODE_PAR_NOT_SINGLE_GROUP, PARSER_STAGE_TRANSLATE);
221

X
Xiaoyu Wang 已提交
222
  run("SELECT c1 FROM t1 order by COUNT(*)", TSDB_CODE_PAR_NOT_SINGLE_GROUP, PARSER_STAGE_TRANSLATE);
223 224

  // TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION
X
Xiaoyu Wang 已提交
225
  run("SELECT distinct c1, c2 FROM t1 WHERE c1 > 0 order by ts", TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION,
226 227
      PARSER_STAGE_TRANSLATE);

X
Xiaoyu Wang 已提交
228
  run("SELECT distinct c1 FROM t1 WHERE c1 > 0 order by COUNT(c2)", TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION,
229 230
      PARSER_STAGE_TRANSLATE);

X
Xiaoyu Wang 已提交
231
  run("SELECT distinct c2 FROM t1 WHERE c1 > 0 order by COUNT(c2)", TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION,
232 233 234
      PARSER_STAGE_TRANSLATE);
}

X
Xiaoyu Wang 已提交
235 236 237
TEST_F(ParserSelectTest, setOperator) {
  useDb("root", "test");

238
  // run("SELECT * FROM t1 UNION ALL SELECT * FROM t1");
X
Xiaoyu Wang 已提交
239

240 241 242
  // run("(SELECT * FROM t1) UNION ALL (SELECT * FROM t1)");

  run("SELECT c1 FROM (SELECT c1 FROM t1 UNION ALL SELECT c1 FROM t1)");
X
Xiaoyu Wang 已提交
243 244
}

245
}  // namespace ParserTest