diff --git a/cql3/restrictions/statement_restrictions.cc b/cql3/restrictions/statement_restrictions.cc index 18c277b161..edaf1979ad 100644 --- a/cql3/restrictions/statement_restrictions.cc +++ b/cql3/restrictions/statement_restrictions.cc @@ -481,7 +481,7 @@ statement_restrictions::statement_restrictions(data_dictionary::database db, _has_multi_column = find_binop(_clustering_columns_restrictions->expression, expr::is_multi_column); _has_queriable_ck_index = _clustering_columns_restrictions->has_supporting_index(sim, allow_local) && !type.is_delete(); - _has_queriable_pk_index = _partition_key_restrictions->has_supporting_index(sim, allow_local) + _has_queriable_pk_index = parition_key_restrictions_have_supporting_index(sim, allow_local) && !type.is_delete(); _has_queriable_regular_index = _nonprimary_key_restrictions->has_supporting_index(sim, allow_local) && !type.is_delete(); @@ -914,6 +914,29 @@ bool statement_restrictions::has_unrestricted_clustering_columns() const { return _clustering_columns_restrictions->has_unrestricted_components(*_schema); } +bool statement_restrictions::parition_key_restrictions_have_supporting_index(const secondary_index::secondary_index_manager& index_manager, + expr::allow_local_index allow_local) const { + // Token restrictions can't be supported by an index + if (has_token(_new_partition_key_restrictions)) { + return false; + } + + // Otherwise those are single column restrictions + std::vector restricted_pk_columns = expr::get_sorted_column_defs(_new_partition_key_restrictions); + + for (const column_definition* cdef : restricted_pk_columns) { + expr::expression col_restrictions = expr::conjunction { + .children = expr::extract_single_column_restrictions_for_column(_new_partition_key_restrictions, *cdef) + }; + + if (expr::has_supporting_index(col_restrictions, index_manager, allow_local)) { + return true; + } + } + + return false; +} + void statement_restrictions::process_clustering_columns_restrictions(bool for_view, bool allow_filtering) { if (!has_clustering_columns_restriction()) { return; diff --git a/cql3/restrictions/statement_restrictions.hh b/cql3/restrictions/statement_restrictions.hh index 460d52ba13..2baa90e624 100644 --- a/cql3/restrictions/statement_restrictions.hh +++ b/cql3/restrictions/statement_restrictions.hh @@ -229,6 +229,8 @@ public: size_t partition_key_restrictions_size() const; + bool parition_key_restrictions_have_supporting_index(const secondary_index::secondary_index_manager& index_manager, expr::allow_local_index allow_local) const; + /** * Checks if the clustering key has some unrestricted components. * @return true if the clustering key has some unrestricted components, false otherwise.