2017-07-15 04:06:51 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-09-07 17:09:37 +00:00
|
|
|
#include <Analyzer/IQueryTreeNode.h>
|
2022-09-04 15:20:59 +00:00
|
|
|
#include <Analyzer/TableExpressionModifiers.h>
|
2023-02-03 13:34:18 +00:00
|
|
|
#include <Core/Names.h>
|
|
|
|
#include <Core/SortDescription.h>
|
|
|
|
#include <Interpreters/AggregateDescription.h>
|
|
|
|
#include <Interpreters/DatabaseAndTableWithAlias.h>
|
|
|
|
#include <Interpreters/PreparedSets.h>
|
2022-09-07 17:09:37 +00:00
|
|
|
#include <Planner/PlannerContext.h>
|
2023-02-03 13:34:18 +00:00
|
|
|
#include <QueryPipeline/StreamLocalLimits.h>
|
|
|
|
#include <Storages/ProjectionsDescription.h>
|
2021-02-10 14:12:49 +00:00
|
|
|
|
2017-07-15 04:06:51 +00:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-04-06 13:58:06 +00:00
|
|
|
class ExpressionActions;
|
|
|
|
using ExpressionActionsPtr = std::shared_ptr<ExpressionActions>;
|
|
|
|
|
2020-11-03 11:28:28 +00:00
|
|
|
class ActionsDAG;
|
|
|
|
using ActionsDAGPtr = std::shared_ptr<ActionsDAG>;
|
|
|
|
|
2021-02-13 22:07:13 +00:00
|
|
|
struct PrewhereInfo;
|
|
|
|
using PrewhereInfoPtr = std::shared_ptr<PrewhereInfo>;
|
|
|
|
|
|
|
|
struct FilterInfo;
|
|
|
|
using FilterInfoPtr = std::shared_ptr<FilterInfo>;
|
|
|
|
|
|
|
|
struct FilterDAGInfo;
|
|
|
|
using FilterDAGInfoPtr = std::shared_ptr<FilterDAGInfo>;
|
|
|
|
|
|
|
|
struct InputOrderInfo;
|
|
|
|
using InputOrderInfoPtr = std::shared_ptr<const InputOrderInfo>;
|
|
|
|
|
|
|
|
struct TreeRewriterResult;
|
|
|
|
using TreeRewriterResultPtr = std::shared_ptr<const TreeRewriterResult>;
|
|
|
|
|
|
|
|
class ReadInOrderOptimizer;
|
|
|
|
using ReadInOrderOptimizerPtr = std::shared_ptr<const ReadInOrderOptimizer>;
|
|
|
|
|
|
|
|
class Cluster;
|
|
|
|
using ClusterPtr = std::shared_ptr<Cluster>;
|
|
|
|
|
2021-08-16 12:09:18 +00:00
|
|
|
struct MergeTreeDataSelectAnalysisResult;
|
|
|
|
using MergeTreeDataSelectAnalysisResultPtr = std::shared_ptr<MergeTreeDataSelectAnalysisResult>;
|
|
|
|
|
2018-04-11 14:31:54 +00:00
|
|
|
struct PrewhereInfo
|
|
|
|
{
|
2021-02-15 19:48:06 +00:00
|
|
|
/// Actions for row level security filter. Applied separately before prewhere_actions.
|
|
|
|
/// This actions are separate because prewhere condition should not be executed over filtered rows.
|
2021-06-25 14:49:28 +00:00
|
|
|
ActionsDAGPtr row_level_filter;
|
2018-04-11 14:31:54 +00:00
|
|
|
/// Actions which are executed on block in order to get filter column for prewhere step.
|
2020-11-03 19:05:47 +00:00
|
|
|
ActionsDAGPtr prewhere_actions;
|
2021-02-15 19:48:06 +00:00
|
|
|
String row_level_column_name;
|
2020-11-03 19:05:47 +00:00
|
|
|
String prewhere_column_name;
|
|
|
|
bool remove_prewhere_column = false;
|
|
|
|
bool need_filter = false;
|
|
|
|
|
2021-06-25 14:49:28 +00:00
|
|
|
PrewhereInfo() = default;
|
|
|
|
explicit PrewhereInfo(ActionsDAGPtr prewhere_actions_, String prewhere_column_name_)
|
2020-11-03 19:05:47 +00:00
|
|
|
: prewhere_actions(std::move(prewhere_actions_)), prewhere_column_name(std::move(prewhere_column_name_)) {}
|
2020-12-02 18:16:31 +00:00
|
|
|
|
|
|
|
std::string dump() const;
|
2022-07-21 15:52:33 +00:00
|
|
|
|
|
|
|
PrewhereInfoPtr clone() const
|
|
|
|
{
|
|
|
|
PrewhereInfoPtr prewhere_info = std::make_shared<PrewhereInfo>();
|
|
|
|
|
|
|
|
if (row_level_filter)
|
|
|
|
prewhere_info->row_level_filter = row_level_filter->clone();
|
|
|
|
|
|
|
|
if (prewhere_actions)
|
|
|
|
prewhere_info->prewhere_actions = prewhere_actions->clone();
|
|
|
|
|
|
|
|
prewhere_info->row_level_column_name = row_level_column_name;
|
|
|
|
prewhere_info->prewhere_column_name = prewhere_column_name;
|
|
|
|
prewhere_info->remove_prewhere_column = remove_prewhere_column;
|
|
|
|
prewhere_info->need_filter = need_filter;
|
|
|
|
|
|
|
|
return prewhere_info;
|
|
|
|
}
|
2020-11-03 19:05:47 +00:00
|
|
|
};
|
|
|
|
|
2019-03-29 20:31:06 +00:00
|
|
|
/// Helper struct to store all the information about the filter expression.
|
|
|
|
struct FilterInfo
|
|
|
|
{
|
2021-02-14 17:16:40 +00:00
|
|
|
ExpressionActionsPtr alias_actions;
|
2021-02-13 22:07:13 +00:00
|
|
|
ExpressionActionsPtr actions;
|
|
|
|
String column_name;
|
|
|
|
bool do_remove_column = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Same as FilterInfo, but with ActionsDAG.
|
|
|
|
struct FilterDAGInfo
|
|
|
|
{
|
|
|
|
ActionsDAGPtr actions;
|
2019-03-29 20:31:06 +00:00
|
|
|
String column_name;
|
|
|
|
bool do_remove_column = false;
|
2020-12-02 18:16:31 +00:00
|
|
|
|
|
|
|
std::string dump() const;
|
2019-03-29 20:31:06 +00:00
|
|
|
};
|
|
|
|
|
2020-05-13 13:49:10 +00:00
|
|
|
struct InputOrderInfo
|
2019-07-18 14:41:11 +00:00
|
|
|
{
|
2022-07-04 15:05:52 +00:00
|
|
|
/// Sort description for merging of already sorted streams.
|
|
|
|
/// Always a prefix of ORDER BY or GROUP BY description specified in query.
|
2022-07-01 16:43:40 +00:00
|
|
|
SortDescription sort_description_for_merging;
|
2022-07-04 15:05:52 +00:00
|
|
|
|
|
|
|
/** Size of prefix of sorting key that is already
|
|
|
|
* sorted before execution of sorting or aggreagation.
|
|
|
|
*
|
|
|
|
* Contains both columns that scpecified in
|
|
|
|
* ORDER BY or GROUP BY clause of query
|
|
|
|
* and columns that turned out to be already sorted.
|
|
|
|
*
|
|
|
|
* E.g. if we have sorting key ORDER BY (a, b, c, d)
|
|
|
|
* and query with `WHERE a = 'x' AND b = 'y' ORDER BY c, d` clauses.
|
|
|
|
* sort_description_for_merging will be equal to (c, d) and
|
|
|
|
* used_prefix_of_sorting_key_size will be equal to 4.
|
|
|
|
*/
|
2022-09-13 12:06:56 +00:00
|
|
|
const size_t used_prefix_of_sorting_key_size;
|
2022-07-01 16:43:40 +00:00
|
|
|
|
2022-09-13 12:06:56 +00:00
|
|
|
const int direction;
|
|
|
|
const UInt64 limit;
|
2019-07-19 10:14:27 +00:00
|
|
|
|
2021-12-23 15:02:32 +00:00
|
|
|
InputOrderInfo(
|
2022-07-01 16:43:40 +00:00
|
|
|
const SortDescription & sort_description_for_merging_,
|
|
|
|
size_t used_prefix_of_sorting_key_size_,
|
2021-12-23 15:02:32 +00:00
|
|
|
int direction_, UInt64 limit_)
|
2022-07-01 16:43:40 +00:00
|
|
|
: sort_description_for_merging(sort_description_for_merging_)
|
|
|
|
, used_prefix_of_sorting_key_size(used_prefix_of_sorting_key_size_)
|
2021-12-23 15:02:32 +00:00
|
|
|
, direction(direction_), limit(limit_)
|
2019-12-10 23:18:24 +00:00
|
|
|
{
|
|
|
|
}
|
2019-12-11 01:34:39 +00:00
|
|
|
|
2021-12-23 15:02:32 +00:00
|
|
|
bool operator==(const InputOrderInfo &) const = default;
|
2019-07-18 14:41:11 +00:00
|
|
|
};
|
|
|
|
|
2021-04-22 13:32:17 +00:00
|
|
|
class IMergeTreeDataPart;
|
|
|
|
|
2021-05-02 13:38:19 +00:00
|
|
|
using ManyExpressionActions = std::vector<ExpressionActionsPtr>;
|
|
|
|
|
2021-04-29 07:38:47 +00:00
|
|
|
// The projection selected to execute current query
|
|
|
|
struct ProjectionCandidate
|
|
|
|
{
|
2021-08-27 18:35:13 +00:00
|
|
|
ProjectionDescriptionRawPtr desc{};
|
2021-04-29 07:38:47 +00:00
|
|
|
PrewhereInfoPtr prewhere_info;
|
|
|
|
ActionsDAGPtr before_where;
|
2021-05-04 12:40:34 +00:00
|
|
|
String where_column_name;
|
2021-05-04 10:52:37 +00:00
|
|
|
bool remove_where_filter = false;
|
2021-04-29 07:38:47 +00:00
|
|
|
ActionsDAGPtr before_aggregation;
|
|
|
|
Names required_columns;
|
|
|
|
NamesAndTypesList aggregation_keys;
|
|
|
|
AggregateDescriptions aggregate_descriptions;
|
2021-05-04 10:52:37 +00:00
|
|
|
bool aggregate_overflow_row = false;
|
|
|
|
bool aggregate_final = false;
|
2021-05-02 13:38:19 +00:00
|
|
|
bool complete = false;
|
|
|
|
ReadInOrderOptimizerPtr order_optimizer;
|
|
|
|
InputOrderInfoPtr input_order_info;
|
|
|
|
ManyExpressionActions group_by_elements_actions;
|
2022-02-03 12:47:27 +00:00
|
|
|
SortDescription group_by_elements_order_descr;
|
2021-08-16 12:09:18 +00:00
|
|
|
MergeTreeDataSelectAnalysisResultPtr merge_tree_projection_select_result_ptr;
|
|
|
|
MergeTreeDataSelectAnalysisResultPtr merge_tree_normal_select_result_ptr;
|
2023-01-02 17:00:48 +00:00
|
|
|
|
2023-01-02 18:01:53 +00:00
|
|
|
/// Because projection analysis uses a separate interpreter.
|
2023-01-02 17:00:48 +00:00
|
|
|
ContextPtr context;
|
2021-04-29 07:38:47 +00:00
|
|
|
};
|
|
|
|
|
2017-07-15 04:06:51 +00:00
|
|
|
/** Query along with some additional data,
|
|
|
|
* that can be used during query processing
|
|
|
|
* inside storage engines.
|
|
|
|
*/
|
2022-07-20 14:45:07 +00:00
|
|
|
struct SelectQueryInfo
|
2017-07-15 04:06:51 +00:00
|
|
|
{
|
2022-07-20 14:45:07 +00:00
|
|
|
SelectQueryInfo()
|
|
|
|
: prepared_sets(std::make_shared<PreparedSets>())
|
|
|
|
{}
|
|
|
|
|
2017-07-15 04:06:51 +00:00
|
|
|
ASTPtr query;
|
2020-06-15 12:36:10 +00:00
|
|
|
ASTPtr view_query; /// Optimized VIEW query
|
2021-10-19 14:09:43 +00:00
|
|
|
ASTPtr original_query; /// Unmodified query for projection analysis
|
2017-07-15 04:06:51 +00:00
|
|
|
|
2022-12-23 17:45:28 +00:00
|
|
|
/// Query tree
|
|
|
|
QueryTreeNodePtr query_tree;
|
|
|
|
|
2022-09-07 17:09:37 +00:00
|
|
|
/// Planner context
|
|
|
|
PlannerContextPtr planner_context;
|
|
|
|
|
|
|
|
/// Storage table expression
|
2023-03-07 20:39:26 +00:00
|
|
|
/// It's guaranteed to be present in JOIN TREE of `query_tree`
|
2022-09-07 17:09:37 +00:00
|
|
|
QueryTreeNodePtr table_expression;
|
|
|
|
|
2022-09-04 15:20:59 +00:00
|
|
|
/// Table expression modifiers for storage
|
|
|
|
std::optional<TableExpressionModifiers> table_expression_modifiers;
|
|
|
|
|
2022-05-31 14:43:38 +00:00
|
|
|
std::shared_ptr<const StorageLimitsList> storage_limits;
|
|
|
|
|
2022-12-23 17:45:28 +00:00
|
|
|
/// Local storage limits
|
|
|
|
StorageLimits local_storage_limits;
|
|
|
|
|
2021-03-29 19:02:34 +00:00
|
|
|
/// Cluster for the query.
|
2020-09-20 17:52:17 +00:00
|
|
|
ClusterPtr cluster;
|
2021-03-29 19:02:34 +00:00
|
|
|
/// Optimized cluster for the query.
|
|
|
|
/// In case of optimize_skip_unused_shards it may differs from original cluster.
|
|
|
|
///
|
|
|
|
/// Configured in StorageDistributed::getQueryProcessingStage()
|
|
|
|
ClusterPtr optimized_cluster;
|
2023-03-03 15:14:49 +00:00
|
|
|
/// should we use custom key with the cluster
|
|
|
|
bool use_custom_key = false;
|
2020-09-10 19:55:36 +00:00
|
|
|
|
2020-07-22 17:13:05 +00:00
|
|
|
TreeRewriterResultPtr syntax_analyzer_result;
|
2018-11-08 15:43:14 +00:00
|
|
|
|
2022-06-21 11:24:46 +00:00
|
|
|
/// This is an additional filer applied to current table.
|
|
|
|
ASTPtr additional_filter_ast;
|
|
|
|
|
2022-07-28 09:40:09 +00:00
|
|
|
/// It is needed for PK analysis based on row_level_policy and additional_filters.
|
|
|
|
ASTs filter_asts;
|
2018-04-06 13:58:06 +00:00
|
|
|
|
2023-01-10 12:28:13 +00:00
|
|
|
ASTPtr parallel_replica_custom_key_ast;
|
|
|
|
|
2022-11-17 18:44:26 +00:00
|
|
|
/// Filter actions dag for current storage
|
|
|
|
ActionsDAGPtr filter_actions_dag;
|
|
|
|
|
2020-05-13 13:49:10 +00:00
|
|
|
ReadInOrderOptimizerPtr order_optimizer;
|
2020-09-20 17:52:17 +00:00
|
|
|
/// Can be modified while reading from storage
|
|
|
|
InputOrderInfoPtr input_order_info;
|
2019-12-10 23:18:24 +00:00
|
|
|
|
2017-07-15 04:06:51 +00:00
|
|
|
/// Prepared sets are used for indices by storage engine.
|
|
|
|
/// Example: x IN (1, 2, 3)
|
2022-07-20 14:45:07 +00:00
|
|
|
PreparedSetsPtr prepared_sets;
|
2021-03-29 19:02:34 +00:00
|
|
|
|
2022-07-01 13:36:20 +00:00
|
|
|
/// Cached value of ExpressionAnalysisResult
|
2021-05-05 21:26:14 +00:00
|
|
|
bool has_window = false;
|
2022-07-01 13:36:20 +00:00
|
|
|
bool has_order_by = false;
|
|
|
|
bool need_aggregate = false;
|
|
|
|
PrewhereInfoPtr prewhere_info;
|
2021-05-05 21:26:14 +00:00
|
|
|
|
2022-12-23 17:45:28 +00:00
|
|
|
/// If query has aggregate functions
|
|
|
|
bool has_aggregates = false;
|
|
|
|
|
2021-03-29 19:02:34 +00:00
|
|
|
ClusterPtr getCluster() const { return !optimized_cluster ? cluster : optimized_cluster; }
|
2021-02-10 14:12:49 +00:00
|
|
|
|
2021-04-25 02:26:36 +00:00
|
|
|
/// If not null, it means we choose a projection to execute current query.
|
2021-04-29 07:38:47 +00:00
|
|
|
std::optional<ProjectionCandidate> projection;
|
2021-04-21 16:00:27 +00:00
|
|
|
bool ignore_projections = false;
|
2021-07-07 05:01:30 +00:00
|
|
|
bool is_projection_query = false;
|
2021-08-26 11:01:15 +00:00
|
|
|
bool merge_tree_empty_result = false;
|
2022-01-11 12:19:41 +00:00
|
|
|
bool settings_limit_offset_done = false;
|
2023-05-08 10:01:24 +00:00
|
|
|
bool is_internal = false;
|
2021-08-27 18:35:13 +00:00
|
|
|
Block minmax_count_projection_block;
|
2021-08-16 12:09:18 +00:00
|
|
|
MergeTreeDataSelectAnalysisResultPtr merge_tree_select_result_ptr;
|
2017-07-15 04:06:51 +00:00
|
|
|
|
2022-10-19 16:30:03 +00:00
|
|
|
bool is_parameterized_view = false;
|
|
|
|
|
2023-08-21 12:30:52 +00:00
|
|
|
bool optimize_trivial_count = false;
|
2022-10-19 16:30:03 +00:00
|
|
|
|
2022-10-23 21:22:52 +00:00
|
|
|
// If limit is not 0, that means it's a trivial limit query.
|
2022-10-22 16:27:24 +00:00
|
|
|
UInt64 limit = 0;
|
|
|
|
|
2023-08-02 20:34:18 +00:00
|
|
|
/// For IStorageSystemOneBlock
|
|
|
|
std::vector<UInt8> columns_mask;
|
|
|
|
|
2022-07-29 14:36:36 +00:00
|
|
|
InputOrderInfoPtr getInputOrderInfo() const
|
|
|
|
{
|
|
|
|
return input_order_info ? input_order_info : (projection ? projection->input_order_info : nullptr);
|
|
|
|
}
|
2023-05-23 18:36:02 +00:00
|
|
|
|
|
|
|
bool isFinal() const;
|
2022-03-30 03:04:31 +00:00
|
|
|
};
|
2017-07-15 04:06:51 +00:00
|
|
|
}
|