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-keyspace-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/yql/cql/ql/test/ql-test-base.h"
19
#include "yb/yql/cql/ql/util/errcodes.h"
20
21
namespace yb {
22
namespace ql {
23
24
class TestQLKeyspace : public QLTestBase {
25
 public:
26
0
  TestQLKeyspace() : QLTestBase() {
27
0
  }
28
29
  // CREATE statements.
30
0
  inline const string CreateStmt(string params) {
31
0
    return "CREATE " + params;
32
0
  }
33
34
0
  inline const string CreateKeyspaceStmt(string params) {
35
0
    return CreateStmt("KEYSPACE " + params);
36
0
  }
37
38
0
  inline const string CreateKeyspaceIfNotExistsStmt(string params) {
39
0
    return CreateStmt("KEYSPACE IF NOT EXISTS " + params);
40
0
  }
41
42
0
  inline const string CreateSchemaStmt(string params) {
43
0
    return CreateStmt("SCHEMA " + params);
44
0
  }
45
46
0
  inline const string CreateSchemaIfNotExistsStmt(string params) {
47
0
    return CreateStmt("SCHEMA IF NOT EXISTS " + params);
48
0
  }
49
50
  // DROP statements.
51
0
  inline const string DropKeyspaceStmt(string params) {
52
0
    return "DROP KEYSPACE " + params;
53
0
  }
54
55
0
  inline const string DropKeyspaceIfExistsStmt(string params) {
56
0
    return "DROP KEYSPACE IF EXISTS " + params;
57
0
  }
58
59
0
  inline const string DropSchemaStmt(string params) {
60
0
    return "DROP SCHEMA " + params;
61
0
  }
62
63
0
  inline const string DropSchemaIfExistsStmt(string params) {
64
0
    return "DROP SCHEMA IF EXISTS " + params;
65
0
  }
66
67
0
  inline const string UseStmt(string params) {
68
0
    return "USE " + params;
69
0
  }
70
71
0
  inline const string CreateTableStmt(string params) {
72
0
    return "CREATE TABLE " + params;
73
0
  }
74
};
75
76
0
TEST_F(TestQLKeyspace, TestQLCreateKeyspaceSimple) {
77
  // Init the simulated cluster.
78
0
  ASSERT_NO_FATALS(CreateSimulatedCluster());
79
80
  // Get an available processor.
81
0
  TestQLProcessor *processor = GetQLProcessor();
82
83
0
  const string keyspace1 = "test;";
84
85
  // Try to delete unknown keyspace1.
86
0
  LOG(INFO) << "Exec SQL: " << DropKeyspaceStmt(keyspace1);
87
0
  EXEC_INVALID_STMT_WITH_ERROR(DropKeyspaceStmt(keyspace1),
88
0
      "Keyspace Not Found. Keyspace name not found");
89
90
  // Delete unknown keyspace1 BUT with IF EXISTS.
91
0
  LOG(INFO) << "Exec SQL: " << DropKeyspaceIfExistsStmt(keyspace1);
92
0
  EXEC_VALID_STMT(DropKeyspaceIfExistsStmt(keyspace1));
93
94
  // Create the keyspace1.
95
0
  LOG(INFO) << "Exec SQL: " << CreateKeyspaceStmt(keyspace1);
96
0
  EXEC_VALID_STMT(CreateKeyspaceStmt(keyspace1));
97
98
  // Try to create the keyspace1 once again.
99
0
  LOG(INFO) << "Exec SQL: " << CreateKeyspaceStmt(keyspace1);
100
0
  EXEC_INVALID_STMT_WITH_ERROR(CreateKeyspaceStmt(keyspace1),
101
0
      "Keyspace Already Exists. Keyspace 'test' already exists");
102
103
  // Delete the keyspace1.
104
0
  LOG(INFO) << "Exec SQL: " << DropKeyspaceStmt(keyspace1);
105
0
  EXEC_VALID_STMT(DropKeyspaceStmt(keyspace1));
106
107
  // Try to delete already deleted keyspace1.
108
0
  LOG(INFO) << "Exec SQL: " << DropKeyspaceStmt(keyspace1);
109
0
  EXEC_INVALID_STMT_WITH_ERROR(DropKeyspaceStmt(keyspace1),
110
0
      "Keyspace Not Found. Keyspace name not found");
111
112
  // Delete already deleted keyspace1 BUT with IF EXISTS.
113
0
  LOG(INFO) << "Exec SQL: " << DropKeyspaceIfExistsStmt(keyspace1);
114
0
  EXEC_VALID_STMT(DropKeyspaceIfExistsStmt(keyspace1));
115
116
  // Try to create a keyspace with a syntax error ('KEYSPAC' instead of 'KEYSPACE').
117
0
  LOG(INFO) << "Exec SQL: " << CreateStmt("KEYSPAC " + keyspace1);
118
0
  EXEC_INVALID_STMT_WITH_ERROR(CreateStmt("KEYSPAC " + keyspace1),
119
0
      "Invalid SQL Statement. syntax error");
120
121
  // Try to create 2 keyspaces in one request.
122
0
  LOG(INFO) << "Exec SQL: " << CreateKeyspaceStmt("ks1 ks2;");
123
0
  EXEC_INVALID_STMT_WITH_ERROR(CreateKeyspaceStmt("ks1 ks2;"),
124
0
      "Invalid SQL Statement. syntax error");
125
126
  // Try to create a keyspaces with unsupported AUTHORIZATION keyword.
127
0
  LOG(INFO) << "Exec SQL: " << CreateKeyspaceStmt("ks1 AUTHORIZATION user1;");
128
0
  EXEC_INVALID_STMT_WITH_ERROR(CreateKeyspaceStmt("ks1 AUTHORIZATION user1;"),
129
0
      "Feature Not Supported");
130
0
}
131
132
0
TEST_F(TestQLKeyspace, TestQLCreateKeyspaceIfNotExists) {
133
  // Init the simulated cluster.
134
0
  ASSERT_NO_FATALS(CreateSimulatedCluster());
135
136
  // Get an available processor.
137
0
  TestQLProcessor *processor = GetQLProcessor();
138
139
0
  const string keyspace1 = "test;";
140
141
  // Create the keyspace1.
142
0
  LOG(INFO) << "Exec SQL: " << CreateKeyspaceIfNotExistsStmt(keyspace1);
143
0
  EXEC_VALID_STMT(CreateKeyspaceIfNotExistsStmt(keyspace1));
144
145
  // Try to create the keyspace1 once again.
146
0
  LOG(INFO) << "Exec SQL: " << CreateKeyspaceIfNotExistsStmt(keyspace1);
147
0
  EXEC_VALID_STMT(CreateKeyspaceIfNotExistsStmt(keyspace1));
148
149
  // Try to create a keyspace with a syntax error ('EXIST' instead of 'EXISTS').
150
0
  LOG(INFO) << "Exec SQL: " << CreateKeyspaceStmt("IF NOT EXIST " + keyspace1);
151
0
  EXEC_INVALID_STMT_WITH_ERROR(CreateKeyspaceStmt("IF NOT EXIST " + keyspace1),
152
0
      "Invalid SQL Statement. syntax error");
153
154
  // THE FOLLOWING TESTS FAIL DUE TO UNKNOWN PARSER BUG. TODO: Investigate
155
156
  // Try to create 2 keyspaces in one request.
157
//  LOG(INFO) << "Exec SQL: " << CreateKeyspaceIfNotExistsStmt("ks1 ks2;");
158
//  EXEC_INVALID_STMT_WITH_ERROR(CreateKeyspaceIfNotExistsStmt("ks1 ks2;"),
159
//      "Invalid SQL Statement. syntax error");
160
161
  // Try to create a keyspaces with unsupported AUTHORIZATION keyword.
162
//  LOG(INFO) << "Exec SQL: " << CreateKeyspaceIfNotExistsStmt("ks1 AUTHORIZATION user1;");
163
//  EXEC_INVALID_STMT_WITH_ERROR(CreateKeyspaceIfNotExistsStmt("ks1 AUTHORIZATION user1;"),
164
//      "Feature Not Supported. AUTHORIZATION");
165
0
}
166
167
0
TEST_F(TestQLKeyspace, TestQLCreateSchemaSimple) {
168
  // Init the simulated cluster.
169
0
  ASSERT_NO_FATALS(CreateSimulatedCluster());
170
171
  // Get an available processor.
172
0
  TestQLProcessor *processor = GetQLProcessor();
173
174
0
  const string keyspace1 = "test;";
175
176
  // Try to delete unknown keyspace1.
177
0
  LOG(INFO) << "Exec SQL: " << DropSchemaStmt(keyspace1);
178
0
  EXEC_INVALID_STMT_WITH_ERROR(DropSchemaStmt(keyspace1),
179
0
      "Keyspace Not Found. Keyspace name not found");
180
181
  // Delete unknown keyspace1 BUT with IF EXISTS.
182
0
  LOG(INFO) << "Exec SQL: " << DropSchemaIfExistsStmt(keyspace1);
183
0
  EXEC_VALID_STMT(DropSchemaIfExistsStmt(keyspace1));
184
185
  // Create the keyspace1.
186
0
  LOG(INFO) << "Exec SQL: " << CreateSchemaStmt(keyspace1);
187
0
  EXEC_VALID_STMT(CreateSchemaStmt(keyspace1));
188
189
  // Try to create the keyspace1 once again.
190
0
  LOG(INFO) << "Exec SQL: " << CreateSchemaStmt(keyspace1);
191
0
  EXEC_INVALID_STMT_WITH_ERROR(CreateSchemaStmt(keyspace1),
192
0
      "Keyspace Already Exists. Keyspace 'test' already exists");
193
194
  // Delete the keyspace1.
195
0
  LOG(INFO) << "Exec SQL: " << DropSchemaStmt(keyspace1);
196
0
  EXEC_VALID_STMT(DropSchemaStmt(keyspace1));
197
198
  // Try to delete already deleted keyspace1.
199
0
  LOG(INFO) << "Exec SQL: " << DropSchemaStmt(keyspace1);
200
0
  EXEC_INVALID_STMT_WITH_ERROR(DropSchemaStmt(keyspace1),
201
0
      "Keyspace Not Found. Keyspace name not found");
202
203
  // Delete already deleted keyspace1 BUT with IF EXISTS.
204
0
  LOG(INFO) << "Exec SQL: " << DropSchemaIfExistsStmt(keyspace1);
205
0
  EXEC_VALID_STMT(DropSchemaIfExistsStmt(keyspace1));
206
207
  // Try to create the keyspace1 with a syntax error ('SCHEM' instead of 'SCHEMA').
208
0
  LOG(INFO) << "Exec SQL: " << CreateStmt("SCHEM " + keyspace1);
209
0
  EXEC_INVALID_STMT_WITH_ERROR(CreateStmt("SCHEM " + keyspace1),
210
0
      "Invalid SQL Statement. syntax error");
211
212
  // Try to create 2 keyspaces in one request.
213
0
  LOG(INFO) << "Exec SQL: " << CreateSchemaStmt("ks1 ks2;");
214
0
  EXEC_INVALID_STMT_WITH_ERROR(CreateSchemaStmt("ks1 ks2;"),
215
0
      "Invalid SQL Statement. syntax error");
216
217
  // Try to create a keyspaces with unsupported AUTHORIZATION keyword.
218
0
  LOG(INFO) << "Exec SQL: " << CreateSchemaStmt("ks1 AUTHORIZATION user1;");
219
0
  EXEC_INVALID_STMT_WITH_ERROR(CreateSchemaStmt("ks1 AUTHORIZATION user1;"),
220
0
      "Feature Not Supported");
221
0
}
222
223
0
TEST_F(TestQLKeyspace, TestQLCreateSchemaIfNotExists) {
224
  // Init the simulated cluster.
225
0
  ASSERT_NO_FATALS(CreateSimulatedCluster());
226
227
  // Get an available processor.
228
0
  TestQLProcessor *processor = GetQLProcessor();
229
230
0
  const string keyspace1 = "test;";
231
232
  // Create the keyspace1.
233
0
  LOG(INFO) << "Exec SQL: " << CreateSchemaIfNotExistsStmt(keyspace1);
234
0
  EXEC_VALID_STMT(CreateSchemaIfNotExistsStmt(keyspace1));
235
236
  // Try to create the keyspace1 once again.
237
0
  LOG(INFO) << "Exec SQL: " << CreateSchemaIfNotExistsStmt(keyspace1);
238
0
  EXEC_VALID_STMT(CreateSchemaIfNotExistsStmt(keyspace1));
239
240
  // Try to create a keyspace with a syntax error ('EXIST' instead of 'EXISTS').
241
0
  LOG(INFO) << "Exec SQL: " << CreateSchemaStmt("IF NOT EXIST " + keyspace1);
242
0
  EXEC_INVALID_STMT_WITH_ERROR(CreateSchemaStmt("IF NOT EXIST " + keyspace1),
243
0
      "Invalid SQL Statement. syntax error");
244
245
  // THE FOLLOWING TESTS FAIL DUE TO UNKNOWN PARSER BUG. TODO: Investigate
246
247
  // Try to create 2 keyspaces in one request.
248
//  LOG(INFO) << "Exec SQL: " << CreateSchemaIfNotExistsStmt("ks1 ks2;");
249
//  EXEC_INVALID_STMT_WITH_ERROR(CreateSchemaIfNotExistsStmt("ks1 ks2;"),
250
//      "Invalid SQL Statement. syntax error");
251
252
  // Try to create a keyspaces with unsupported AUTHORIZATION keyword.
253
//  LOG(INFO) << "Exec SQL: " << CreateSchemaIfNotExistsStmt("ks1 AUTHORIZATION user1;");
254
//  EXEC_INVALID_STMT_WITH_ERROR(CreateSchemaIfNotExistsStmt("ks1 AUTHORIZATION user1;"),
255
//      "Feature Not Supported. AUTHORIZATION");
256
0
}
257
258
0
TEST_F(TestQLKeyspace, TestQLUseKeyspaceSimple) {
259
  // Init the simulated cluster.
260
0
  ASSERT_NO_FATALS(CreateSimulatedCluster());
261
262
  // Get an available processor.
263
0
  TestQLProcessor *processor = GetQLProcessor();
264
265
0
  const string keyspace1 = "test;";
266
267
  // Try to use unknown keyspace1.
268
0
  LOG(INFO) << "Exec SQL: " << UseStmt(keyspace1);
269
0
  EXEC_INVALID_STMT_WITH_ERROR(UseStmt(keyspace1),
270
0
      "Keyspace Not Found. Cannot use unknown keyspace");
271
272
  // Create the keyspace1.
273
0
  LOG(INFO) << "Exec SQL: " << CreateKeyspaceStmt(keyspace1);
274
0
  EXEC_VALID_STMT(CreateKeyspaceStmt(keyspace1));
275
276
  // Use the keyspace1.
277
0
  LOG(INFO) << "Exec SQL: " << UseStmt(keyspace1);
278
0
  EXEC_VALID_STMT(UseStmt(keyspace1));
279
280
  // Delete keyspace1.
281
0
  LOG(INFO) << "Exec SQL: " << DropKeyspaceStmt(keyspace1);
282
0
  EXEC_VALID_STMT(DropKeyspaceStmt(keyspace1));
283
284
  // Try to use deleted keyspace1.
285
0
  LOG(INFO) << "Exec SQL: " << UseStmt(keyspace1);
286
0
  EXEC_INVALID_STMT_WITH_ERROR(UseStmt(keyspace1),
287
0
      "Keyspace Not Found. Cannot use unknown keyspace");
288
0
}
289
290
0
TEST_F(TestQLKeyspace, TestQLUseKeyspaceWithTable) {
291
  // Init the simulated cluster.
292
0
  ASSERT_NO_FATALS(CreateSimulatedCluster());
293
294
  // Get an available processor.
295
0
  TestQLProcessor *processor = GetQLProcessor();
296
297
0
  const string keyspace1 = "test;";
298
0
  const string keyspace2 = "test2;";
299
0
  const string table1 = "table1(id int, primary key(id));";
300
0
  const string system_table2 = "system.table2(id int, primary key(id));";
301
0
  const string test_table3 = "test.table3(id int, primary key(id));";
302
0
  const string table3 = "table3(id int, primary key(id));";
303
0
  const string test_any_table4 = "test.subname.table4(id int, primary key(id));";
304
305
  // No keyspace - using current keyspace (kDefaultKeyspaceName here).
306
0
  LOG(INFO) << "Exec SQL: " << CreateTableStmt(table1);
307
0
  EXEC_VALID_STMT(CreateTableStmt(table1));
308
309
  // 'system' keyspace (not supported yet)
310
0
  LOG(INFO) << "Exec SQL: " << CreateTableStmt(system_table2);
311
0
  EXEC_INVALID_STMT_WITH_ERROR(CreateTableStmt(system_table2),
312
0
                               ErrorText(ErrorCode::SYSTEM_NAMESPACE_READONLY));
313
314
  // 'default' keyspace is always available.
315
  // TODO: It's failed now because 'DEFAULT' is a reserved keyword. Discuss & fix the case.
316
  // LOG(INFO) << "Exec SQL: " << CreateTableStmt(default_table5);
317
  // EXEC_VALID_STMT(CreateTableStmt(default_table5));
318
319
  // The keyspace (keyspace1) has not been created yet.
320
0
  LOG(INFO) << "Exec SQL: " << CreateTableStmt(test_table3);
321
0
  EXEC_INVALID_STMT_WITH_ERROR(CreateTableStmt(test_table3),
322
0
      "Keyspace Not Found. Error creating table test.table3 on the master: "
323
0
      "Keyspace name not found: test");
324
325
  // Invalid name 'keyspace.SOMETHING.table'.
326
0
  LOG(INFO) << "Exec SQL: " << CreateTableStmt(test_any_table4);
327
0
  EXEC_INVALID_STMT_WITH_ERROR(CreateTableStmt(test_any_table4), "Invalid table name");
328
329
  // Create the keyspace1.
330
0
  LOG(INFO) << "Exec SQL: " << CreateKeyspaceStmt(keyspace1);
331
0
  EXEC_VALID_STMT(CreateKeyspaceStmt(keyspace1));
332
333
  // Create table in the new keyspace.
334
0
  LOG(INFO) << "Exec SQL: " << CreateTableStmt(test_table3);
335
0
  EXEC_VALID_STMT(CreateTableStmt(test_table3));
336
337
  // Use the keyspace1.
338
0
  LOG(INFO) << "Exec SQL: " << UseStmt(keyspace1);
339
0
  EXEC_VALID_STMT(UseStmt(keyspace1));
340
341
  // Use current keyspace. The table has been already created.
342
0
  LOG(INFO) << "Exec SQL: " << CreateTableStmt(table3);
343
0
  EXEC_INVALID_STMT_WITH_ERROR(CreateTableStmt(table3),
344
0
      "Duplicate Object. Object 'test.table3' already exists");
345
346
  // Create the keyspace2.
347
0
  LOG(INFO) << "Exec SQL: " << CreateKeyspaceStmt(keyspace2);
348
0
  EXEC_VALID_STMT(CreateKeyspaceStmt(keyspace2));
349
350
  // Use the keyspace2.
351
0
  LOG(INFO) << "Exec SQL: " << UseStmt(keyspace2);
352
0
  EXEC_VALID_STMT(UseStmt(keyspace2));
353
354
  // Table3 can be created in other (keyspace2) keyspace.
355
0
  LOG(INFO) << "Exec SQL: " << CreateTableStmt(table3);
356
0
  EXEC_VALID_STMT(CreateTableStmt(table3));
357
0
}
358
359
0
TEST_F(TestQLKeyspace, TestCreateSystemTable) {
360
  // Init the simulated cluster.
361
0
  ASSERT_NO_FATALS(CreateSimulatedCluster());
362
363
  // Get an available processor.
364
0
  TestQLProcessor *processor = GetQLProcessor();
365
366
  // Allow writes to system keyspace.
367
0
  client::FLAGS_yb_system_namespace_readonly = false;
368
369
  // Create system table.
370
0
  EXEC_VALID_STMT("create table system.t (c int primary key, v int);");
371
372
  // Insert into system table.
373
0
  EXEC_VALID_STMT("insert into system.t (c, v) values (1, 2);");
374
375
  // Select from system table.
376
0
  EXEC_VALID_STMT("select * from system.t where c = 1;");
377
0
  std::shared_ptr<QLRowBlock> row_block = processor->row_block();
378
0
  CHECK_EQ(row_block->row_count(), 1);
379
0
  const QLRow& row = row_block->row(0);
380
0
  CHECK_EQ(row.column(0).int32_value(), 1);
381
0
  CHECK_EQ(row.column(1).int32_value(), 2);
382
0
}
383
384
0
TEST_F(TestQLKeyspace, TestQLSelectInvalidTable) {
385
  // Init the simulated cluster.
386
0
  ASSERT_NO_FATALS(CreateSimulatedCluster());
387
388
  // Get an available processor.
389
0
  TestQLProcessor *processor = GetQLProcessor();
390
391
0
  const string select_stmt = "SELECT * FROM my_keyspace1.test_table WHERE h1 = 1 AND h2 = 'h1';";
392
393
0
  LOG(INFO) << "Exec SQL: " << select_stmt;
394
0
  EXEC_INVALID_STMT_WITH_ERROR(select_stmt, "Object Not Found");
395
0
}
396
397
} // namespace ql
398
} // namespace yb