/*
* Copyright (C) 2016 ScyllaDB
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see .
*/
#include
#include
#include "database.hh"
#include "types/user.hh"
#include "db/view/node_view_update_backlog.hh"
#include "db/view/view_builder.hh"
#include
#include
#include "tests/cql_test_env.hh"
#include "tests/cql_assertions.hh"
#include "exceptions/unrecognized_entity_exception.hh"
#include "db/config.hh"
using namespace std::literals::chrono_literals;
// CQL usually folds identifier names - keyspace, table and column names -
// to lowercase. That is, unless the identifier is enclosed in double
// quotation marks (") then the identifier becomes case sensitive.
// Let's test that case-sensitive (quoted) column names can be used for
// materialized views. Test that data can be inserted and queried, and
// that case sensitive columns in views can be renamed.
// This test reproduces issues #3388 and #3391.
SEASTAR_TEST_CASE(test_case_sensitivity) {
return do_with_cql_env_thread([] (auto& e) {
e.execute_cql("create table cf (\"theKey\" int, \"theClustering\" int, \"theValue\" int, primary key (\"theKey\", \"theClustering\"));").get();
e.execute_cql("create materialized view mv_test as select * from cf "
"where \"theKey\" is not null and \"theClustering\" is not null and \"theValue\" is not null "
"primary key (\"theKey\",\"theClustering\")").get();
e.execute_cql("create materialized view mv_test2 as select \"theKey\", \"theClustering\", \"theValue\" from cf "
"where \"theKey\" is not null and \"theClustering\" is not null and \"theValue\" is not null "
"primary key (\"theKey\",\"theClustering\")").get();
e.execute_cql("insert into cf (\"theKey\", \"theClustering\", \"theValue\") values (0 ,0, 0);").get();
for (auto view : {"mv_test", "mv_test2"}) {
eventually([&] {
auto msg = e.execute_cql(format("select \"theKey\", \"theClustering\", \"theValue\" from {} ", view)).get0();
assert_that(msg).is_rows()
.with_size(1)
.with_row({
{int32_type->decompose(0)},
{int32_type->decompose(0)},
{int32_type->decompose(0)},
});
});
}
e.execute_cql("alter table cf rename \"theClustering\" to \"Col\";").get();
for (auto view : {"mv_test", "mv_test2"}) {
eventually([&] {
auto msg = e.execute_cql(format("select \"theKey\", \"Col\", \"theValue\" from {} ", view)).get0();
assert_that(msg).is_rows()
.with_size(1)
.with_row({
{int32_type->decompose(0)},
{int32_type->decompose(0)},
{int32_type->decompose(0)},
});
});
}
});
}
SEASTAR_TEST_CASE(test_access_and_schema) {
return do_with_cql_env_thread([] (auto& e) {
e.execute_cql("create table cf (p int, c ascii, v bigint, primary key (p, c));").get();
e.execute_cql("create materialized view vcf as select * from cf "
"where v is not null and p is not null and c is not null "
"primary key (v, p, c)").get();
e.execute_cql("insert into cf (p, c, v) values (0, 'foo', 1);").get();
assert_that_failed(e.execute_cql("insert into vcf (p, c, v) values (1, 'foo', 1);"));
assert_that_failed(e.execute_cql("alter table vcf add foo text;"));
assert_that_failed(e.execute_cql("alter table vcf with compaction = { 'class' : 'LeveledCompactionStrategy' };"));
e.execute_cql("alter materialized view vcf with compaction = { 'class' : 'LeveledCompactionStrategy' };").get();
e.execute_cql("alter table cf add foo text;").get();
e.execute_cql("insert into cf (p, c, v, foo) values (0, 'foo', 1, 'bar');").get();
eventually([&] {
auto msg = e.execute_cql("select foo from vcf").get0();
assert_that(msg).is_rows()
.with_size(1)
.with_row({
{utf8_type->decompose(sstring("bar"))},
});
});
e.execute_cql("alter table cf rename c to bar;").get();
auto msg = e.execute_cql("select bar from vcf").get0();
assert_that(msg).is_rows()
.with_size(1)
.with_row({
{utf8_type->decompose(sstring("foo"))},
});
});
}
SEASTAR_TEST_CASE(test_updates) {
return do_with_cql_env_thread([] (auto& e) {
e.execute_cql("create table base (k int, v int, primary key (k));").get();
e.execute_cql("create materialized view mv as select * from base "
"where k is not null and v is not null primary key (v, k)").get();
e.execute_cql("insert into base (k, v) values (0, 0);").get();
auto msg = e.execute_cql("select k, v from base where k = 0").get0();
assert_that(msg).is_rows()
.with_size(1)
.with_row({ {int32_type->decompose(0)}, {int32_type->decompose(0)} });
eventually([&] {
auto msg = e.execute_cql("select k, v from mv where v = 0").get0();
assert_that(msg).is_rows()
.with_size(1)
.with_row({ {int32_type->decompose(0)}, {int32_type->decompose(0)} });
});
e.execute_cql("insert into base (k, v) values (0, 1);").get();
msg = e.execute_cql("select k, v from base where k = 0").get0();
assert_that(msg).is_rows()
.with_size(1)
.with_row({ {int32_type->decompose(0)}, {int32_type->decompose(1)} });
eventually([&] {
auto msg = e.execute_cql("select k, v from mv where v = 0").get0();
assert_that(msg).is_rows().with_size(0);
msg = e.execute_cql("select k, v from mv where v = 1").get0();
assert_that(msg).is_rows()
.with_size(1)
.with_row({ {int32_type->decompose(0)}, {int32_type->decompose(1)} });
});
});
}
SEASTAR_TEST_CASE(test_updates_no_read_before_update) {
return do_with_cql_env_thread([] (auto& e) {
e.execute_cql("create table base (k int, c int, v int, primary key (k));").get();
e.execute_cql("create materialized view mv as select * from base "
"where k is not null and c is not null primary key (k, c)").get();
e.execute_cql("insert into base (k, c, v) values (0, 0, 0);").get();
auto msg = e.execute_cql("select k, v from base where k = 0").get0();
assert_that(msg).is_rows()
.with_size(1)
.with_row({ {int32_type->decompose(0)}, {int32_type->decompose(0)} });
eventually([&] {
auto msg = e.execute_cql("select k, v from mv where k = 0").get0();
assert_that(msg).is_rows()
.with_size(1)
.with_row({ {int32_type->decompose(0)}, {int32_type->decompose(0)} });
});
e.execute_cql("insert into base (k, c, v) values (0, 0, 1);").get();
msg = e.execute_cql("select k, v from base where k = 0").get0();
assert_that(msg).is_rows()
.with_size(1)
.with_row({ {int32_type->decompose(0)}, {int32_type->decompose(1)} });
eventually([&] {
msg = e.execute_cql("select k, v from mv where k = 0").get0();
assert_that(msg).is_rows()
.with_size(1)
.with_row({ {int32_type->decompose(0)}, {int32_type->decompose(1)} });
});
});
}
SEASTAR_TEST_CASE(test_reuse_name) {
return do_with_cql_env_thread([] (auto& e) {
e.execute_cql("create table cf (p int primary key, v int);").get();
e.execute_cql("create materialized view vcf as select * from cf "
"where v is not null and p is not null primary key (v, p)").get();
e.execute_cql("drop materialized view vcf").get();
e.execute_cql("create materialized view vcf as select * from cf "
"where v is not null and p is not null "
"primary key (v, p)").get();
});
}
SEASTAR_TEST_CASE(test_all_types) {
logging::logger_registry().set_all_loggers_level(logging::log_level::error);
return do_with_cql_env_thread([] (auto& e) {
e.execute_cql("CREATE TYPE myType (a int, b uuid, c set)").get();
e.execute_cql("CREATE TABLE cf ("
"k int PRIMARY KEY, "
"asciival ascii, "
"bigintval bigint, "
"blobval blob, "
"booleanval boolean, "
"dateval date, "
"decimalval decimal, "
"doubleval double, "
"floatval float, "
"inetval inet, "
"intval int, "
"textval text, "
"timeval time, "
"timestampval timestamp, "
"timeuuidval timeuuid, "
"uuidval uuid,"
"varcharval varchar, "
"varintval varint, "
"listval list, "
"frozenlistval frozen>, "
"setval set, "
"frozensetval frozen>, "
"mapval map,"
"frozenmapval frozen