Files
scylladb/cql3/statements/raw/select_statement.hh
Avi Kivity d768e9fac5 cql3, related: switch to data_dictionary
Stop using database (and including database.hh) for schema related
purposes and use data_dictionary instead.

data_dictionary::database::real_database() is called from several
places, for these reasons:

 - calling yet-to-be-converted code
 - callers with a legitimate need to access data (e.g. system_keyspace)
   but with the ::database accessor removed from query_processor.
   We'll need to find another way to supply system_keyspace with
   data access.
 - to gain access to the wasm engine for testing whether used
   defined functions compile. We'll have to find another way to
   do this as well.

The change is a straightforward replacement. One case in
modification_statement had to change a capture, but everything else
was just a search-and-replace.

Some files that lost "database.hh" gained "mutation.hh", which they
previously had access to through "database.hh".
2021-12-15 13:54:23 +02:00

207 lines
7.8 KiB
C++

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright (C) 2015-present ScyllaDB
*
* Modified by ScyllaDB
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "cql3/statements/raw/cf_statement.hh"
#include "cql3/statements/prepared_statement.hh"
#include "cql3/relation.hh"
#include "cql3/attributes.hh"
#include "db/config.hh"
#include <seastar/core/shared_ptr.hh>
namespace cql3 {
namespace selection {
class selection;
class raw_selector;
} // namespace selection
namespace restrictions {
class statement_restrictions;
} // namespace restrictions
namespace statements {
namespace raw {
/**
* Encapsulates a completely parsed SELECT query, including the target
* column family, expression, result count, and ordering clause.
*
*/
class select_statement : public cf_statement
{
public:
// Ordering of selected values as defined by the basic comparison order.
// Even for a column that by default has ordering 4, 3, 2, 1 ordering it in ascending order will result in 1, 2, 3, 4.
enum class ordering {
ascending,
descending
};
class parameters final {
public:
using orderings_type = std::vector<std::pair<shared_ptr<column_identifier::raw>, ordering>>;
private:
const orderings_type _orderings;
const bool _is_distinct;
const bool _allow_filtering;
const bool _is_json;
bool _bypass_cache = false;
public:
parameters();
parameters(orderings_type orderings,
bool is_distinct,
bool allow_filtering);
parameters(orderings_type orderings,
bool is_distinct,
bool allow_filtering,
bool is_json,
bool bypass_cache);
bool is_distinct() const;
bool allow_filtering() const;
bool is_json() const;
bool bypass_cache() const;
orderings_type const& orderings() const;
};
template<typename T>
using compare_fn = std::function<bool(const T&, const T&)>;
using result_row_type = std::vector<bytes_opt>;
using ordering_comparator_type = compare_fn<result_row_type>;
private:
using prepared_orderings_type = std::vector<std::pair<const column_definition*, ordering>>;
private:
lw_shared_ptr<const parameters> _parameters;
std::vector<::shared_ptr<selection::raw_selector>> _select_clause;
std::vector<::shared_ptr<relation>> _where_clause;
std::optional<expr::expression> _limit;
std::optional<expr::expression> _per_partition_limit;
std::vector<::shared_ptr<cql3::column_identifier::raw>> _group_by_columns;
std::unique_ptr<cql3::attributes::raw> _attrs;
public:
select_statement(cf_name cf_name,
lw_shared_ptr<const parameters> parameters,
std::vector<::shared_ptr<selection::raw_selector>> select_clause,
std::vector<::shared_ptr<relation>> where_clause,
std::optional<expr::expression> limit,
std::optional<expr::expression> per_partition_limit,
std::vector<::shared_ptr<cql3::column_identifier::raw>> group_by_columns,
std::unique_ptr<cql3::attributes::raw> attrs);
virtual std::unique_ptr<prepared_statement> prepare(data_dictionary::database db, cql_stats& stats) override {
return prepare(db, stats, false);
}
std::unique_ptr<prepared_statement> prepare(data_dictionary::database db, cql_stats& stats, bool for_view);
private:
void maybe_jsonize_select_clause(data_dictionary::database db, schema_ptr schema);
::shared_ptr<restrictions::statement_restrictions> prepare_restrictions(
data_dictionary::database db,
schema_ptr schema,
prepare_context& ctx,
::shared_ptr<selection::selection> selection,
bool for_view = false,
bool allow_filtering = false);
/** Returns an expression for the limit or nullopt if no limit is set */
std::optional<expr::expression> prepare_limit(data_dictionary::database db, prepare_context& ctx, const std::optional<expr::expression>& limit);
// Checks whether it is legal to have ORDER BY in this statement
static void verify_ordering_is_allowed(const restrictions::statement_restrictions& restrictions);
void handle_unrecognized_ordering_column(const column_identifier& column) const;
// Processes ORDER BY column orderings, converts column_identifiers to column_defintions
prepared_orderings_type prepare_orderings(const schema& schema) const;
void verify_ordering_is_valid(const prepared_orderings_type&, const schema&, const restrictions::statement_restrictions& restrictions) const;
// Checks whether this ordering reverses all results.
// We only allow leaving select results unchanged or reversing them.
bool is_ordering_reversed(const prepared_orderings_type&) const;
select_statement::ordering_comparator_type get_ordering_comparator(
const prepared_orderings_type&,
selection::selection& selection,
const restrictions::statement_restrictions& restrictions);
static void validate_distinct_selection(const schema& schema,
const selection::selection& selection,
const restrictions::statement_restrictions& restrictions);
/** If ALLOW FILTERING was not specified, this verifies that it is not needed */
void check_needs_filtering(
const restrictions::statement_restrictions& restrictions,
db::tri_mode_restriction_t::mode strict_allow_filtering,
std::vector<sstring>& warnings);
void ensure_filtering_columns_retrieval(data_dictionary::database db,
selection::selection& selection,
const restrictions::statement_restrictions& restrictions);
/// Returns indices of GROUP BY cells in fetched rows.
std::vector<size_t> prepare_group_by(const schema& schema, selection::selection& selection) const;
bool contains_alias(const column_identifier& name) const;
lw_shared_ptr<column_specification> limit_receiver(bool per_partition = false);
#if 0
public:
virtual sstring to_string() override {
return sstring("raw_statement(")
+ "name=" + cf_name->to_string()
+ ", selectClause=" + to_string(_select_clause)
+ ", whereClause=" + to_string(_where_clause)
+ ", isDistinct=" + to_string(_parameters->is_distinct())
+ ", isJson=" + to_string(_parameters->is_json())
+ ")";
}
};
#endif
};
}
}
}