Files
scylladb/cql3/prepare_context.hh
Jan Ciolek 3cff10f756 cql3/prepare_context: fix generating pk_indexes for duplicate named bind variables
When presented with queries that use the same named bind variables twice,
like this one:
```cql
SELECT p FROM table WHERE p = :x AND c = :x
```

Scylla generated empty partition_key_bind_indexes (pk_indexes).
pk_indexes tell the driver which bind variables it should use to calculate the partition
token for a query. Without it, the driver is unable to determine the token and it will
send the query to a random node.

Scylla should generate pk_indexes which tell the driver that it can use bind variable
with bind_index = 0 to calculate the partition token for a query.

The problem was that _target_columns keep only a single target_column for each bind variable.
In the example above :x is compared with both p and c, but _target_columns would contain
only one of them, and Scylla wasn't able to tell that this bind variable is compared with
a partition key column.

To fix it, let's replace _target_columns with _targets. _targets keeps all comparisons
between bind variables and other expressions, so none of them will be forgotten/overwritten.

Fixes: #15374

Signed-off-by: Jan Ciolek <jan.ciolek@scylladb.com>
2023-09-25 17:18:53 +02:00

93 lines
3.3 KiB
C++

/*
* Copyright (C) 2015-present ScyllaDB
*
* Modified by ScyllaDB
*/
/*
* SPDX-License-Identifier: (AGPL-3.0-or-later and Apache-2.0)
*/
#pragma once
#include <seastar/core/shared_ptr.hh>
#include "seastarx.hh"
#include <optional>
#include <vector>
#include <stddef.h>
#include "cql3/expr/expression.hh"
class schema;
namespace cql3 {
class column_identifier;
class column_specification;
namespace functions { class function_call; }
/**
* Metadata class currently holding bind variables specifications and
* `function_call` AST nodes inside a query partition key restrictions.
* Populated and maintained at "prepare" step of query execution.
*/
class prepare_context final {
private:
// Keeps names of all the bind variables. For bind variables without a name ('?'), the name is nullptr.
// Maps bind_index -> name.
std::vector<shared_ptr<column_identifier>> _variable_names;
// Keeps column_specification for every bind_index. column_specification describes the name and type of this variable.
std::vector<lw_shared_ptr<column_specification>> _variable_specs;
// For every expression like (<target> = <bind variable>), there's a pair of (bind_index, target column_specification) in _targets.
// Collecting all equalities of bind variables allows to determine which of the variables set the value of partition key columns.
// The driver needs this information in order to compute the partition token and send the request to the right node.
std::vector<std::pair<std::size_t, lw_shared_ptr<column_specification>>> _targets;
// A list of pointers to prepared `function_call` cache ids, that
// participate in partition key ranges computation within an LWT statement.
std::vector<::shared_ptr<std::optional<uint8_t>>> _pk_function_calls_cache_ids;
// The flag denoting whether the context is currently in partition key
// processing mode (inside query restrictions AST nodes). If set to true,
// then every `function_call` instance will be recorded in the context and
// will be assigned an identifier, which will then be used for caching
// the function call results.
bool _processing_pk_restrictions = false;
public:
prepare_context() = default;
size_t bound_variables_size() const;
const std::vector<lw_shared_ptr<column_specification>>& get_variable_specifications() const &;
std::vector<lw_shared_ptr<column_specification>> get_variable_specifications() &&;
std::vector<uint16_t> get_partition_key_bind_indexes(const schema& schema) const;
void add_variable_specification(int32_t bind_index, lw_shared_ptr<column_specification> spec);
void set_bound_variables(const std::vector<shared_ptr<column_identifier>>& bind_variable_names);
void clear_pk_function_calls_cache();
// Record a new function call, which evaluates a partition key constraint.
// Also automatically assigns an id to the AST node for caching purposes.
void add_pk_function_call(cql3::expr::function_call& fn);
// Inform the context object that it has started or ended processing the
// partition key part of statement restrictions.
void set_processing_pk_restrictions(bool flag) noexcept {
_processing_pk_restrictions = flag;
}
bool is_processing_pk_restrictions() const noexcept {
return _processing_pk_restrictions;
}
};
}