From d04d7bd409292364c6bc173e358040a90c6f7e79 Mon Sep 17 00:00:00 2001 From: Piotr Sarna Date: Wed, 12 Jun 2019 08:59:57 +0200 Subject: [PATCH] cql3: fix fetching clustering key columns for filtering When a column is not present in the select clause, but used for filtering, it usually needs to be fetched from replicas. Sometimes it can be avoided, e.g. if primary key columns form a valid prefix - then, they will be optimized out before filtering itself. However, clustering key prefix can only be qualified for this optimization if the whole partition key is restricted - otherwise the clustering columns still need to be present for filtering. Fixes #4541 --- cql3/restrictions/statement_restrictions.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cql3/restrictions/statement_restrictions.cc b/cql3/restrictions/statement_restrictions.cc index 535b34a138a7..f74de4e9ba14 100644 --- a/cql3/restrictions/statement_restrictions.cc +++ b/cql3/restrictions/statement_restrictions.cc @@ -392,8 +392,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)) {