YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/yql/cql/ql/test/ql-arith-test.cc
Line
Count
Source (jump to first uncovered line)
1
//--------------------------------------------------------------------------------------------------
2
// Copyright (c) YugaByte, Inc.
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
// in compliance with the License.  You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software distributed under the License
10
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
// or implied.  See the License for the specific language governing permissions and limitations
12
// under the License.
13
//
14
//--------------------------------------------------------------------------------------------------
15
16
#include "yb/common/ql_value.h"
17
18
#include "yb/gutil/strings/substitute.h"
19
20
#include "yb/yql/cql/ql/test/ql-test-base.h"
21
22
using std::string;
23
using std::unique_ptr;
24
using std::shared_ptr;
25
using strings::Substitute;
26
27
namespace yb {
28
namespace ql {
29
30
class TestQLArith : public QLTestBase {
31
 public:
32
0
  TestQLArith() : QLTestBase() {
33
0
  }
34
};
35
36
0
TEST_F(TestQLArith, TestQLArithVarint) {
37
  // Init the simulated cluster.
38
0
  ASSERT_NO_FATALS(CreateSimulatedCluster());
39
40
  // Get a processor.
41
0
  TestQLProcessor *processor = GetQLProcessor();
42
0
  LOG(INFO) << "Running simple query test.";
43
  // Create the table 1.
44
0
  const char *create_stmt =
45
0
      "CREATE TABLE test_varint(h1 int primary key, v1 varint, v2 varint, v3 varint);";
46
0
  CHECK_VALID_STMT(create_stmt);
47
48
  // Simple varint update
49
0
  CHECK_VALID_STMT("UPDATE test_varint SET v1 = 70000000000007, v2 = v2 + 77 WHERE h1 = 1;");
50
51
  // Checking Row
52
0
  CHECK_VALID_STMT("SELECT * FROM test_varint WHERE h1 = 1");
53
0
  std::shared_ptr<QLRowBlock> row_block = processor->row_block();
54
0
  CHECK_EQ(row_block->row_count(), 1);
55
0
  const QLRow& row = row_block->row(0);
56
0
  CHECK_EQ(row.column(0).int32_value(), 1);
57
0
  CHECK_EQ(row.column(1).varint_value(), util::VarInt(70000000000007));
58
0
  CHECK(row.column(2).IsNull());
59
0
  CHECK(row.column(3).IsNull());
60
61
  // Simple varint update
62
0
  CHECK_VALID_STMT("UPDATE test_varint SET v1 = v1 + 20, v2 = v1 + v1, v3 = v1 + v2 WHERE h1 = 1;");
63
64
  // Checking Row
65
0
  CHECK_VALID_STMT("SELECT * FROM test_varint WHERE h1 = 1");
66
0
  row_block = processor->row_block();
67
0
  CHECK_EQ(row_block->row_count(), 1);
68
0
  const QLRow& new_row = row_block->row(0);
69
0
  CHECK_EQ(new_row.column(0).int32_value(), 1);
70
0
  CHECK_EQ(new_row.column(1).varint_value(), util::VarInt(70000000000027));
71
0
  CHECK_EQ(new_row.column(2).varint_value(), util::VarInt(140000000000014));
72
0
  CHECK(new_row.column(3).IsNull());
73
0
}
74
75
0
TEST_F(TestQLArith, TestQLArithBigInt) {
76
  // Init the simulated cluster.
77
0
  ASSERT_NO_FATALS(CreateSimulatedCluster());
78
79
  // Get a processor.
80
0
  TestQLProcessor *processor = GetQLProcessor();
81
0
  LOG(INFO) << "Running simple query test.";
82
  // Create the table 1.
83
0
  const char *create_stmt =
84
0
      "CREATE TABLE test_bigint(h1 int primary key, c1 bigint, c2 bigint, c3 bigint);";
85
0
  CHECK_VALID_STMT(create_stmt);
86
87
  // Simple counter update
88
0
  CHECK_VALID_STMT("UPDATE test_bigint SET c1 = 77, c2 = c2 + 77 WHERE h1 = 1;");
89
90
  // Select counter.
91
0
  CHECK_VALID_STMT("SELECT * FROM test_bigint WHERE h1 = 1");
92
0
  std::shared_ptr<QLRowBlock> row_block = processor->row_block();
93
0
  CHECK_EQ(row_block->row_count(), 1);
94
0
  const QLRow& row = row_block->row(0);
95
0
  CHECK_EQ(row.column(0).int32_value(), 1);
96
0
  CHECK_EQ(row.column(1).int64_value(), 77);
97
0
  CHECK(row.column(2).IsNull());
98
0
  CHECK(row.column(3).IsNull());
99
100
  // Simple counter update
101
0
  CHECK_VALID_STMT("UPDATE test_bigint SET c1 = c1 + 20, c2 = c1 + c1, c3 = c1 + c2 WHERE h1 = 1;");
102
103
  // Select counter.
104
0
  CHECK_VALID_STMT("SELECT * FROM test_bigint WHERE h1 = 1");
105
0
  row_block = processor->row_block();
106
0
  CHECK_EQ(row_block->row_count(), 1);
107
0
  const QLRow& new_row = row_block->row(0);
108
0
  CHECK_EQ(new_row.column(0).int32_value(), 1);
109
0
  CHECK_EQ(new_row.column(1).int64_value(), 97);
110
0
  CHECK_EQ(new_row.column(2).int64_value(), 154);
111
0
  CHECK(new_row.column(3).IsNull());
112
0
}
113
114
0
TEST_F(TestQLArith, TestQLArithInt) {
115
  // Init the simulated cluster.
116
0
  ASSERT_NO_FATALS(CreateSimulatedCluster());
117
118
  // Get a processor.
119
0
  TestQLProcessor *processor = GetQLProcessor();
120
0
  LOG(INFO) << "Running simple query test.";
121
  // Create the table 1.
122
0
  const char *create_stmt =
123
0
    "CREATE TABLE test_int(h1 int primary key, c1 int, c2 int, c3 smallint, c4 tinyint);";
124
0
  CHECK_VALID_STMT(create_stmt);
125
126
  // Simple counter update
127
0
  CHECK_VALID_STMT("UPDATE test_int SET c1 = 77, c2 = c2 + 77 WHERE h1 = 1;");
128
129
  // Select counter.
130
0
  CHECK_VALID_STMT("SELECT * FROM test_int WHERE h1 = 1");
131
0
  std::shared_ptr<QLRowBlock> row_block = processor->row_block();
132
0
  CHECK_EQ(row_block->row_count(), 1);
133
0
  const QLRow& row = row_block->row(0);
134
0
  CHECK_EQ(row.column(0).int32_value(), 1);
135
0
  CHECK_EQ(row.column(1).int32_value(), 77);
136
0
  CHECK(row.column(2).IsNull());
137
0
  CHECK(row.column(3).IsNull());
138
0
  CHECK(row.column(4).IsNull());
139
140
  // Simple counter update
141
0
  CHECK_VALID_STMT("UPDATE test_int SET c1 = c1 + 20, c2 = c1 + c1, c3 = c1 + 3, c4 = c1 + 4"
142
0
                   "  WHERE h1 = 1;");
143
144
  // Select counter.
145
0
  CHECK_VALID_STMT("SELECT * FROM test_int WHERE h1 = 1");
146
0
  row_block = processor->row_block();
147
0
  CHECK_EQ(row_block->row_count(), 1);
148
0
  const QLRow& new_row = row_block->row(0);
149
0
  CHECK_EQ(new_row.column(0).int32_value(), 1);
150
0
  CHECK_EQ(new_row.column(1).int32_value(), 97);
151
0
  CHECK_EQ(new_row.column(2).int32_value(), 154);
152
0
  CHECK_EQ(new_row.column(3).int16_value(), 80);
153
0
  CHECK_EQ(new_row.column(4).int8_value(), 81);
154
0
}
155
156
0
TEST_F(TestQLArith, TestQLArithCounter) {
157
  // Init the simulated cluster.
158
0
  ASSERT_NO_FATALS(CreateSimulatedCluster());
159
160
  // Get a processor.
161
0
  TestQLProcessor *processor = GetQLProcessor();
162
0
  LOG(INFO) << "Running simple query test.";
163
  // Create the table 1.
164
0
  const char *create_stmt =
165
0
    "CREATE TABLE test_counter(h1 int primary key, c1 counter, c2 counter, c3 counter);";
166
0
  CHECK_VALID_STMT(create_stmt);
167
168
  // Simple counter update
169
0
  CHECK_VALID_STMT("UPDATE test_counter SET c1 = c1 + 77 WHERE h1 = 1;");
170
171
  // Select counter.
172
0
  CHECK_VALID_STMT("SELECT * FROM test_counter WHERE h1 = 1");
173
0
  std::shared_ptr<QLRowBlock> row_block = processor->row_block();
174
0
  CHECK_EQ(row_block->row_count(), 1);
175
0
  const QLRow& row = row_block->row(0);
176
0
  CHECK_EQ(row.column(0).int32_value(), 1);
177
0
  CHECK_EQ(row.column(1).int64_value(), 77);
178
179
  // Simple counter update
180
0
  CHECK_VALID_STMT("UPDATE test_counter SET c1 = c1 + 10 WHERE h1 = 1;");
181
182
  // Select counter.
183
0
  CHECK_VALID_STMT("SELECT * FROM test_counter WHERE h1 = 1");
184
0
  row_block = processor->row_block();
185
0
  CHECK_EQ(row_block->row_count(), 1);
186
0
  const QLRow& new_row = row_block->row(0);
187
0
  CHECK_EQ(new_row.column(0).int32_value(), 1);
188
0
  CHECK_EQ(new_row.column(1).int64_value(), 87);
189
0
}
190
191
0
TEST_F(TestQLArith, TestQLErrorArithCounter) {
192
  // Init the simulated cluster.
193
0
  ASSERT_NO_FATALS(CreateSimulatedCluster());
194
195
  // Get a processor.
196
0
  TestQLProcessor *processor = GetQLProcessor();
197
0
  LOG(INFO) << "Running simple query test.";
198
  // Create the table 1.
199
0
  const char *create_stmt =
200
0
    "CREATE TABLE test_counter(h1 int primary key, c1 counter, c2 counter, c3 counter);";
201
0
  CHECK_VALID_STMT(create_stmt);
202
203
  // Insert is not allowed.
204
0
  CHECK_INVALID_STMT("INSERT INTO test_counter(h1, c1) VALUES(1, 2);");
205
206
  // Update with constant.
207
0
  CHECK_INVALID_STMT("UPDATE test_counter SET c1 = 2 WHERE h1 = 1;");
208
209
  // Update with wrong column.
210
0
  CHECK_INVALID_STMT("UPDATE test_counter SET c1 = c2 + 3 WHERE h1 = 2;");
211
0
}
212
213
0
TEST_F(TestQLArith, TestSimpleArithExpr) {
214
  // Init the simulated cluster.
215
0
  ASSERT_NO_FATALS(CreateSimulatedCluster());
216
217
  // Get a processor.
218
0
  TestQLProcessor *processor = GetQLProcessor();
219
220
0
  const char *create_stmt = "CREATE TABLE test_expr(h int, r int, v int, primary key(h, r));";
221
0
  CHECK_VALID_STMT(create_stmt);
222
223
  //------------------------------------------------------------------------------------------------
224
  // Test Insert: VALUES clause.
225
226
  // Hash columns.
227
0
  EXEC_VALID_STMT("INSERT INTO test_expr(h, r, v) VALUES(1 + 1, 1, 1);");
228
229
  // Range columns.
230
0
  EXEC_VALID_STMT("INSERT INTO test_expr(h, r, v) VALUES(1, 1 + 1, 1);");
231
232
  // Regular columns.
233
0
  EXEC_VALID_STMT("INSERT INTO test_expr(h, r, v) VALUES(1, 1, 1 + 1);");
234
235
  // Expressions with wrong type are not allowed.
236
0
  EXEC_INVALID_STMT("INSERT INTO test_expr(h, r, v) VALUES(3, 1, 'a' + 'b');");
237
0
  EXEC_INVALID_STMT("INSERT INTO test_expr(h, r, v) VALUES(2 + 3.0, 1, 1);");
238
239
  // Expressions with column references not allowed as values.
240
0
  CHECK_INVALID_STMT("INSERT INTO test_expr(h, r, v) VALUES(1 + v, 1, 1);");
241
0
  CHECK_INVALID_STMT("INSERT INTO test_expr(h, r, v) VALUES(1, h + 1, 1);");
242
0
  CHECK_INVALID_STMT("INSERT INTO test_expr(h, r, v) VALUES(1, 1, r + 1);");
243
0
  CHECK_INVALID_STMT("INSERT INTO test_expr(h, r, v) VALUES(2, 1, h + r);");
244
245
  //------------------------------------------------------------------------------------------------
246
  // Test Select: SELECT and WHERE clauses.
247
248
  // Test various selects.
249
0
  std::shared_ptr<QLRowBlock> row_block;
250
251
  // Selecting expressions.
252
0
  CHECK_VALID_STMT("SELECT 1 + 2, h, 2 + 3 FROM test_expr WHERE h = 2");
253
0
  CHECK_EQ(processor->row_block()->row_count(), 1);
254
0
  CHECK_EQ(processor->row_block()->row(0).column(0).int64_value(), 3);
255
0
  CHECK_EQ(processor->row_block()->row(0).column(1).int32_value(), 2);
256
0
  CHECK_EQ(processor->row_block()->row(0).column(2).int64_value(), 5);
257
258
  // Expressions for hash key columns.
259
0
  CHECK_VALID_STMT("SELECT h, r, v FROM test_expr WHERE h = 1 + 1");
260
0
  CHECK_EQ(processor->row_block()->row_count(), 1);
261
0
  CHECK_EQ(processor->row_block()->row(0).column(0).int32_value(), 2);
262
263
0
  CHECK_VALID_STMT("SELECT h, r, v FROM test_expr WHERE h = 1 + 1 AND r = 1");
264
0
  CHECK_EQ(processor->row_block()->row_count(), 1);
265
0
  CHECK_EQ(processor->row_block()->row(0).column(0).int32_value(), 2);
266
267
  // Expressions for range key columns.
268
0
  CHECK_VALID_STMT("SELECT h, r, v FROM test_expr WHERE r = 1 + 1");
269
0
  CHECK_EQ(processor->row_block()->row_count(), 1);
270
0
  CHECK_EQ(processor->row_block()->row(0).column(1).int32_value(), 2);
271
272
0
  CHECK_VALID_STMT("SELECT h, r, v FROM test_expr WHERE h = 1 AND r = 1 + 1");
273
0
  CHECK_EQ(processor->row_block()->row_count(), 1);
274
0
  CHECK_EQ(processor->row_block()->row(0).column(1).int32_value(), 2);
275
276
  // Expressions for regular columns.
277
0
  CHECK_VALID_STMT("SELECT h, r, v FROM test_expr WHERE v = 1 + 1;");
278
0
  CHECK_EQ(processor->row_block()->row_count(), 1);
279
0
  CHECK_VALID_STMT("SELECT h, r, v FROM test_expr WHERE h = 1 AND v = 1 + 1;");
280
0
  CHECK_EQ(processor->row_block()->row_count(), 1);
281
0
  CHECK_VALID_STMT("SELECT h, r, v FROM test_expr WHERE r = 1 AND v = 1 + 1;");
282
0
  CHECK_EQ(processor->row_block()->row_count(), 1);
283
0
  CHECK_VALID_STMT("SELECT h, r, v FROM test_expr WHERE h = 1 AND r = 1 AND v = 1 + 1;");
284
0
  CHECK_EQ(processor->row_block()->row_count(), 1);
285
286
  // Expressions with column references are not allowed in RHS of where clause.
287
0
  CHECK_INVALID_STMT("SELECT h, r, v FROM test_expr WHERE h = r + 1");
288
0
  CHECK_INVALID_STMT("SELECT h, r, v FROM test_expr WHERE h = r + 1 AND r = 1");
289
0
  CHECK_INVALID_STMT("SELECT h, r, v FROM test_expr WHERE r = h + 1");
290
0
  CHECK_INVALID_STMT("SELECT h, r, v FROM test_expr WHERE h = 1 AND r = h + 1");
291
0
  CHECK_INVALID_STMT("SELECT h, r, v FROM test_expr WHERE v = r + 1;");
292
0
  CHECK_INVALID_STMT("SELECT h, r, v FROM test_expr WHERE h = 1 AND v = h + 1;");
293
0
  CHECK_INVALID_STMT("SELECT h, r, v FROM test_expr WHERE r = 1 AND v = r + 1;");
294
0
  CHECK_INVALID_STMT("SELECT h, r, v FROM test_expr WHERE h = 1 AND r = 1 AND v = r + 1;");
295
296
  //------------------------------------------------------------------------------------------------
297
  // Test Update: WHERE, IF and SET clauses.
298
299
0
  CHECK_VALID_STMT("UPDATE test_expr SET v = 1 + 2 WHERE h = 1 + 1 AND r = 1;");
300
0
  CHECK_VALID_STMT("UPDATE test_expr SET v = 2 + 1 WHERE h = 1 AND r = 1 + 1;");
301
0
  CHECK_VALID_STMT("UPDATE test_expr SET v = 1 + 2 WHERE h = 1 AND r = 1 IF v = 1 + 1;");
302
303
  // Check rows got updated, all three rows should now have value 3.
304
0
  CHECK_VALID_STMT("SELECT v FROM test_expr;");
305
0
  CHECK_EQ(processor->row_block()->row_count(), 3);
306
0
  CHECK_EQ(processor->row_block()->row(0).column(0).int32_value(), 3);
307
0
  CHECK_EQ(processor->row_block()->row(1).column(0).int32_value(), 3);
308
0
  CHECK_EQ(processor->row_block()->row(2).column(0).int32_value(), 3);
309
310
  // Expressions with column references are not allowed in RHS of if/where clauses.
311
0
  CHECK_INVALID_STMT("UPDATE test_expr SET v = 1 WHERE h = r + 1 AND r = 1;");
312
0
  CHECK_INVALID_STMT("UPDATE test_expr SET v = 1 WHERE h = 1 AND r = h + 1;");
313
0
  CHECK_INVALID_STMT("UPDATE test_expr SET v = 1 WHERE h = 1 AND r = 1 IF v = h + 1;");
314
315
  //------------------------------------------------------------------------------------------------
316
  // Test Delete: WHERE clause.
317
318
0
  CHECK_VALID_STMT("DELETE FROM test_expr where h = 1 + 1 AND r = 1;");
319
0
  CHECK_VALID_STMT("DELETE FROM test_expr where h = 1 AND r = 1 + 1;");
320
0
  CHECK_VALID_STMT("DELETE FROM test_expr where h = 1 AND r = 1 IF v = 1 + 2;");
321
322
  // Check rows got deleted.
323
0
  CHECK_VALID_STMT("SELECT * FROM test_expr;");
324
0
  CHECK_EQ(processor->row_block()->row_count(), 0);
325
326
  // Expressions with column references are not allowed in RHS of if/where clauses.
327
0
  CHECK_INVALID_STMT("DELETE FROM test_expr WHERE h = r + 1 AND r = 1;");
328
0
  CHECK_INVALID_STMT("UPDATE FROM test_expr WHERE h = 1 AND r = h + 1;");
329
0
  CHECK_INVALID_STMT("UPDATE FROM test_expr WHERE h = 1 AND r = 1 IF v = h + 1;");
330
331
0
}
332
333
} // namespace ql
334
} // namespace yb