diff --git a/index/secondary_index_manager.cc b/index/secondary_index_manager.cc index 1024483ba5..99281689b8 100644 --- a/index/secondary_index_manager.cc +++ b/index/secondary_index_manager.cc @@ -134,6 +134,11 @@ view_ptr secondary_index_manager::create_view_for_index(const index_metadata& im } builder.with_column(col.name(), col.type, column_kind::clustering_key); } + if (index_target->is_primary_key()) { + for (auto& def : schema->regular_columns()) { + db::view::create_virtual_column(builder, def.name(), def.type); + } + } const sstring where_clause = format("{} IS NOT NULL", cql3::util::maybe_quote(index_target_name)); builder.with_view_info(*schema, false, where_clause); return view_ptr{builder.build()}; diff --git a/tests/secondary_index_test.cc b/tests/secondary_index_test.cc index a78595a56d..ea1f794810 100644 --- a/tests/secondary_index_test.cc +++ b/tests/secondary_index_test.cc @@ -605,3 +605,16 @@ SEASTAR_TEST_CASE(test_secondary_index_create_custom_index) { assert_that_failed(e.execute_cql("create index on cf (a) using 'org.apache.cassandra.index.sasi.SASIIndex'")); }); } + +// Reproducer for #4144 +SEASTAR_TEST_CASE(test_secondary_index_contains_virtual_columns) { + return do_with_cql_env_thread([] (cql_test_env& e) { + e.execute_cql("create table cf (p int, c int, v int, primary key(p, c))").get(); + e.execute_cql("create index on cf (c)").get(); + e.execute_cql("update cf set v = 1 where p = 1 and c = 1").get(); + eventually([&] { + auto res = e.execute_cql("select * from cf where c = 1").get0(); + assert_that(res).is_rows().with_rows({{{int32_type->decompose(1)}, {int32_type->decompose(1)}, {int32_type->decompose(1)}}}); + }); + }); +} \ No newline at end of file