2018-11-02 18:53:23 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Core/Names.h>
|
|
|
|
#include <Core/NamesAndTypes.h>
|
2019-09-02 19:58:45 +00:00
|
|
|
#include <Core/SettingsCommon.h>
|
|
|
|
#include <Parsers/ASTTablesInSelectQuery.h>
|
2019-09-09 19:43:37 +00:00
|
|
|
#include <Interpreters/IJoin.h>
|
2019-09-03 14:36:02 +00:00
|
|
|
#include <DataStreams/IBlockStream_fwd.h>
|
2019-09-09 19:43:37 +00:00
|
|
|
#include <DataStreams/SizeLimits.h>
|
2018-11-02 18:53:23 +00:00
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class Context;
|
|
|
|
class ASTSelectQuery;
|
2019-02-13 19:00:52 +00:00
|
|
|
struct DatabaseAndTableWithAlias;
|
2019-09-02 19:58:45 +00:00
|
|
|
class Block;
|
|
|
|
|
2019-09-09 19:43:37 +00:00
|
|
|
struct Settings;
|
2018-11-02 18:53:23 +00:00
|
|
|
|
2019-09-03 14:36:02 +00:00
|
|
|
class AnalyzedJoin
|
2018-11-02 18:53:23 +00:00
|
|
|
{
|
|
|
|
/** Query of the form `SELECT expr(x) AS k FROM t1 ANY LEFT JOIN (SELECT expr(x) AS k FROM t2) USING k`
|
|
|
|
* The join is made by column k.
|
|
|
|
* During the JOIN,
|
|
|
|
* - in the "right" table, it will be available by alias `k`, since `Project` action for the subquery was executed.
|
|
|
|
* - in the "left" table, it will be accessible by the name `expr(x)`, since `Project` action has not been executed yet.
|
|
|
|
* You must remember both of these options.
|
|
|
|
*
|
|
|
|
* Query of the form `SELECT ... from t1 ANY LEFT JOIN (SELECT ... from t2) ON expr(t1 columns) = expr(t2 columns)`
|
|
|
|
* to the subquery will be added expression `expr(t2 columns)`.
|
|
|
|
* It's possible to use name `expr(t2 columns)`.
|
|
|
|
*/
|
2019-07-30 18:39:37 +00:00
|
|
|
|
|
|
|
friend class SyntaxAnalyzer;
|
|
|
|
|
2019-09-09 19:43:37 +00:00
|
|
|
const SizeLimits size_limits;
|
|
|
|
const bool join_use_nulls;
|
2019-09-12 18:06:25 +00:00
|
|
|
const bool partial_merge_join;
|
2019-09-23 19:36:47 +00:00
|
|
|
const bool partial_merge_join_optimisations;
|
|
|
|
const size_t partial_merge_join_rows_in_right_blocks;
|
2019-09-09 19:43:37 +00:00
|
|
|
|
2018-11-02 18:53:23 +00:00
|
|
|
Names key_names_left;
|
|
|
|
Names key_names_right; /// Duplicating names are qualified.
|
|
|
|
ASTs key_asts_left;
|
|
|
|
ASTs key_asts_right;
|
2019-09-02 19:58:45 +00:00
|
|
|
ASTTableJoin table_join;
|
2018-11-02 18:53:23 +00:00
|
|
|
|
|
|
|
/// All columns which can be read from joined table. Duplicating names are qualified.
|
2019-07-30 18:39:37 +00:00
|
|
|
NamesAndTypesList columns_from_joined_table;
|
2019-09-02 19:58:45 +00:00
|
|
|
/// Columns will be added to block by JOIN. It's a subset of columns_from_joined_table with corrected Nullability
|
|
|
|
NamesAndTypesList columns_added_by_join;
|
|
|
|
|
2019-07-30 18:39:37 +00:00
|
|
|
/// Name -> original name. Names are the same as in columns_from_joined_table list.
|
|
|
|
std::unordered_map<String, String> original_names;
|
|
|
|
/// Original name -> name. Only ranamed columns.
|
|
|
|
std::unordered_map<String, String> renames;
|
|
|
|
|
|
|
|
public:
|
2019-09-09 19:43:37 +00:00
|
|
|
AnalyzedJoin(const Settings &);
|
|
|
|
|
|
|
|
/// for StorageJoin
|
|
|
|
AnalyzedJoin(SizeLimits limits, bool use_nulls, ASTTableJoin::Kind kind, ASTTableJoin::Strictness strictness,
|
|
|
|
const Names & key_names_right_)
|
|
|
|
: size_limits(limits)
|
|
|
|
, join_use_nulls(use_nulls)
|
2019-09-12 18:06:25 +00:00
|
|
|
, partial_merge_join(false)
|
2019-09-23 19:36:47 +00:00
|
|
|
, partial_merge_join_optimisations(false)
|
|
|
|
, partial_merge_join_rows_in_right_blocks(0)
|
2019-09-09 19:43:37 +00:00
|
|
|
, key_names_right(key_names_right_)
|
|
|
|
{
|
|
|
|
table_join.kind = kind;
|
|
|
|
table_join.strictness = strictness;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTTableJoin::Kind kind() const { return table_join.kind; }
|
|
|
|
ASTTableJoin::Strictness strictness() const { return table_join.strictness; }
|
|
|
|
const SizeLimits & sizeLimits() const { return size_limits; }
|
2019-09-12 14:09:05 +00:00
|
|
|
|
2019-09-18 12:24:35 +00:00
|
|
|
bool forceNullableRight() const { return join_use_nulls && isLeftOrFull(table_join.kind); }
|
|
|
|
bool forceNullableLeft() const { return join_use_nulls && isRightOrFull(table_join.kind); }
|
2019-09-23 19:36:47 +00:00
|
|
|
size_t maxRowsInRightBlock() const { return partial_merge_join_rows_in_right_blocks; }
|
|
|
|
bool enablePartialMergeJoinOptimisations() const { return partial_merge_join_optimisations; }
|
2019-09-09 19:43:37 +00:00
|
|
|
|
2019-02-13 15:18:02 +00:00
|
|
|
void addUsingKey(const ASTPtr & ast);
|
|
|
|
void addOnKeys(ASTPtr & left_table_ast, ASTPtr & right_table_ast);
|
2019-02-06 16:44:47 +00:00
|
|
|
|
2019-09-02 19:58:45 +00:00
|
|
|
bool hasUsing() const { return table_join.using_expression_list != nullptr; }
|
|
|
|
bool hasOn() const { return !hasUsing(); }
|
2019-01-30 15:51:39 +00:00
|
|
|
|
2019-07-30 18:39:37 +00:00
|
|
|
NameSet getQualifiedColumnsSet() const;
|
|
|
|
NameSet getOriginalColumnsSet() const;
|
2019-09-04 16:20:02 +00:00
|
|
|
NamesWithAliases getNamesWithAliases(const NameSet & required_columns) const;
|
|
|
|
NamesWithAliases getRequiredColumns(const Block & sample, const Names & action_columns) const;
|
2018-11-02 18:53:23 +00:00
|
|
|
|
2019-07-30 18:39:37 +00:00
|
|
|
void deduplicateAndQualifyColumnNames(const NameSet & left_table_columns, const String & right_table_prefix);
|
2019-05-13 18:58:15 +00:00
|
|
|
size_t rightKeyInclusion(const String & name) const;
|
2019-09-11 15:57:09 +00:00
|
|
|
NameSet requiredRightKeys() const;
|
2019-09-02 19:58:45 +00:00
|
|
|
|
|
|
|
void addJoinedColumn(const NameAndTypePair & joined_column);
|
|
|
|
void addJoinedColumnsAndCorrectNullability(Block & sample_block) const;
|
|
|
|
|
|
|
|
ASTPtr leftKeysList() const;
|
|
|
|
ASTPtr rightKeysList() const; /// For ON syntax only
|
|
|
|
|
|
|
|
Names requiredJoinedNames() const;
|
|
|
|
const Names & keyNamesLeft() const { return key_names_left; }
|
2019-09-09 19:43:37 +00:00
|
|
|
const Names & keyNamesRight() const { return key_names_right; }
|
2019-09-02 19:58:45 +00:00
|
|
|
const NamesAndTypesList & columnsFromJoinedTable() const { return columns_from_joined_table; }
|
|
|
|
const NamesAndTypesList & columnsAddedByJoin() const { return columns_added_by_join; }
|
|
|
|
|
|
|
|
static bool sameJoin(const AnalyzedJoin * x, const AnalyzedJoin * y);
|
2019-09-16 12:37:46 +00:00
|
|
|
friend JoinPtr makeJoin(std::shared_ptr<AnalyzedJoin> table_join, const Block & right_sample_block);
|
2018-11-02 18:53:23 +00:00
|
|
|
};
|
|
|
|
|
2018-11-07 12:55:19 +00:00
|
|
|
struct ASTTableExpression;
|
2018-11-02 18:53:23 +00:00
|
|
|
NamesAndTypesList getNamesAndTypeListFromTableExpression(const ASTTableExpression & table_expression, const Context & context);
|
|
|
|
|
|
|
|
}
|