diff --git a/cql3/restrictions/primary_key_restrictions.hh b/cql3/restrictions/primary_key_restrictions.hh index 2074192d14..f4c7a56672 100644 --- a/cql3/restrictions/primary_key_restrictions.hh +++ b/cql3/restrictions/primary_key_restrictions.hh @@ -95,7 +95,32 @@ public: uint32_t size() const override { return uint32_t(get_column_defs().size()); } + + bool has_unrestricted_components(const schema& schema) const; + + virtual bool needs_filtering(const schema& schema) const; }; +template<> +inline bool primary_key_restrictions::has_unrestricted_components(const schema& schema) const { + return size() < schema.partition_key_size(); +} + +template<> +inline bool primary_key_restrictions::has_unrestricted_components(const schema& schema) const { + return size() < schema.clustering_key_size(); +} + +template<> +inline bool primary_key_restrictions::needs_filtering(const schema& schema) const { + return !empty() && !is_on_token() && (has_unrestricted_components(schema) || is_contains() || is_slice()); +} + +template<> +inline bool primary_key_restrictions::needs_filtering(const schema& schema) const { + // Currently only overloaded single_column_primary_key_restrictions will require ALLOW FILTERING + return false; +} + } } diff --git a/cql3/restrictions/single_column_primary_key_restrictions.hh b/cql3/restrictions/single_column_primary_key_restrictions.hh index 3d49c50092..43f2ce116d 100644 --- a/cql3/restrictions/single_column_primary_key_restrictions.hh +++ b/cql3/restrictions/single_column_primary_key_restrictions.hh @@ -349,6 +349,8 @@ public: _restrictions->restrictions() | boost::adaptors::map_values, [&] (auto&& r) { return r->is_satisfied_by(schema, key, ckey, cells, options, now); }); } + + virtual bool needs_filtering(const schema& schema) const override; }; template<> @@ -406,6 +408,29 @@ single_column_primary_key_restrictions::bounds_ranges(con return bounds; } +template<> +bool single_column_primary_key_restrictions::needs_filtering(const schema& schema) const { + return primary_key_restrictions::needs_filtering(schema); +} + +template<> +bool single_column_primary_key_restrictions::needs_filtering(const schema& schema) const { + // Restrictions currently need filtering in three cases: + // 1. any of them is a CONTAINS restriction + // 2. restrictions do not form a contiguous prefix (i.e. there are gaps in it) + // 3. a SLICE restriction isn't on a last place + column_id position = 0; + for (const auto& restriction : _restrictions->restrictions() | boost::adaptors::map_values) { + if (restriction->is_contains() || position != restriction->get_column_def().id) { + return true; + } + if (!restriction->is_slice()) { + position = restriction->get_column_def().id + 1; + } + } + return false; +} + } }