diff --git a/cql3/restrictions/statement_restrictions.cc b/cql3/restrictions/statement_restrictions.cc index 315efeae3445..00f2f5104265 100644 --- a/cql3/restrictions/statement_restrictions.cc +++ b/cql3/restrictions/statement_restrictions.cc @@ -363,8 +363,9 @@ std::vector statement_restrictions::get_column_defs_fo } } } - if (_clustering_columns_restrictions->needs_filtering(*_schema)) { - column_id first_filtering_id = _schema->clustering_key_columns().begin()->id + + const bool pk_has_unrestricted_components = _partition_key_restrictions->has_unrestricted_components(*_schema); + if (pk_has_unrestricted_components || _clustering_columns_restrictions->needs_filtering(*_schema)) { + column_id first_filtering_id = pk_has_unrestricted_components ? 0 : _schema->clustering_key_columns().begin()->id + _clustering_columns_restrictions->num_prefix_columns_that_need_not_be_filtered(); for (auto&& cdef : _clustering_columns_restrictions->get_column_defs()) { if (cdef->id >= first_filtering_id && !column_uses_indexing(cdef)) { @@ -479,10 +480,9 @@ bool statement_restrictions::need_filtering() const { int number_of_filtering_restrictions = _nonprimary_key_restrictions->size(); // If the whole partition key is restricted, it does not imply filtering if (_partition_key_restrictions->has_unrestricted_components(*_schema) || !_partition_key_restrictions->is_all_eq()) { - number_of_filtering_restrictions += _partition_key_restrictions->size(); - if (_clustering_columns_restrictions->has_unrestricted_components(*_schema)) { - number_of_filtering_restrictions += _clustering_columns_restrictions->size() - _clustering_columns_restrictions->prefix_size(); - } + number_of_filtering_restrictions += _partition_key_restrictions->size() + _clustering_columns_restrictions->size(); + } else if (_clustering_columns_restrictions->has_unrestricted_components(*_schema)) { + number_of_filtering_restrictions += _clustering_columns_restrictions->size() - _clustering_columns_restrictions->prefix_size(); } if (_partition_key_restrictions->is_multi_column() || _clustering_columns_restrictions->is_multi_column()) { diff --git a/cql3/restrictions/statement_restrictions.hh b/cql3/restrictions/statement_restrictions.hh index 922c6107c365..5eb6a3f8c726 100644 --- a/cql3/restrictions/statement_restrictions.hh +++ b/cql3/restrictions/statement_restrictions.hh @@ -395,6 +395,14 @@ public: return !_nonprimary_key_restrictions->empty(); } + bool pk_restrictions_need_filtering() const { + return _partition_key_restrictions->needs_filtering(*_schema); + } + + bool ck_restrictions_need_filtering() const { + return _partition_key_restrictions->has_unrestricted_components(*_schema) || _clustering_columns_restrictions->needs_filtering(*_schema); + } + /** * @return true if column is restricted by some restriction, false otherwise */ diff --git a/tests/cql_query_test.cc b/tests/cql_query_test.cc index 62eb0da520e9..a2b57424ebf4 100644 --- a/tests/cql_query_test.cc +++ b/tests/cql_query_test.cc @@ -2137,8 +2137,8 @@ SEASTAR_TEST_CASE(test_in_restriction) { return e.execute_cql("select r1 from tir2 where (c1,r1) in ((0, 1),(1,2),(0,1),(1,2),(3,3)) ALLOW FILTERING;"); }).then([&e] (shared_ptr msg) { assert_that(msg).is_rows().with_rows_ignore_order({ - {int32_type->decompose(1)}, - {int32_type->decompose(2)}, + {int32_type->decompose(1), int32_type->decompose(0)}, + {int32_type->decompose(2), int32_type->decompose(1)}, }); }).then([&e] { return e.prepare("select r1 from tir2 where (c1,r1) in ? ALLOW FILTERING;"); @@ -2163,8 +2163,8 @@ SEASTAR_TEST_CASE(test_in_restriction) { return e.execute_prepared(prepared_id,raw_values); }).then([&e] (shared_ptr msg) { assert_that(msg).is_rows().with_rows_ignore_order({ - {int32_type->decompose(1)}, - {int32_type->decompose(2)}, + {int32_type->decompose(1),int32_type->decompose(0)}, + {int32_type->decompose(2), int32_type->decompose(1)}, }); }); }); diff --git a/tests/filtering_test.cc b/tests/filtering_test.cc index b1315e0adc87..8591e079e03a 100644 --- a/tests/filtering_test.cc +++ b/tests/filtering_test.cc @@ -634,6 +634,13 @@ SEASTAR_TEST_CASE(test_allow_filtering_with_secondary_index) { } }); }); + + eventually([&] { + auto msg = e.execute_cql("SELECT SUM(e) FROM t WHERE c = 5 AND b = 911 ALLOW FILTERING;").get0(); + assert_that(msg).is_rows().with_rows({{ int32_type->decompose(0), {} }}); + msg = e.execute_cql("SELECT e FROM t WHERE c = 5 AND b = 3 ALLOW FILTERING;").get0(); + assert_that(msg).is_rows().with_rows({{ int32_type->decompose(9), int32_type->decompose(3) }}); + }); }); }