1#pragma once
2
3#include <Core/Names.h>
4#include <Core/NamesAndTypes.h>
5#include <Parsers/ASTTablesInSelectQuery.h>
6#include <Interpreters/IJoin.h>
7#include <Interpreters/asof.h>
8#include <DataStreams/IBlockStream_fwd.h>
9#include <DataStreams/SizeLimits.h>
10
11#include <utility>
12#include <memory>
13
14namespace DB
15{
16
17class Context;
18class ASTSelectQuery;
19struct DatabaseAndTableWithAlias;
20class Block;
21
22struct Settings;
23
24class AnalyzedJoin
25{
26 /** Query of the form `SELECT expr(x) AS k FROM t1 ANY LEFT JOIN (SELECT expr(x) AS k FROM t2) USING k`
27 * The join is made by column k.
28 * During the JOIN,
29 * - in the "right" table, it will be available by alias `k`, since `Project` action for the subquery was executed.
30 * - in the "left" table, it will be accessible by the name `expr(x)`, since `Project` action has not been executed yet.
31 * You must remember both of these options.
32 *
33 * Query of the form `SELECT ... from t1 ANY LEFT JOIN (SELECT ... from t2) ON expr(t1 columns) = expr(t2 columns)`
34 * to the subquery will be added expression `expr(t2 columns)`.
35 * It's possible to use name `expr(t2 columns)`.
36 */
37
38 friend class SyntaxAnalyzer;
39
40 const SizeLimits size_limits;
41 const size_t default_max_bytes;
42 const bool join_use_nulls;
43 const bool partial_merge_join = false;
44 const bool partial_merge_join_optimizations = false;
45 const size_t partial_merge_join_rows_in_right_blocks = 0;
46
47 Names key_names_left;
48 Names key_names_right; /// Duplicating names are qualified.
49 ASTs key_asts_left;
50 ASTs key_asts_right;
51 ASTTableJoin table_join;
52 ASOF::Inequality asof_inequality = ASOF::Inequality::GreaterOrEquals;
53
54 /// All columns which can be read from joined table. Duplicating names are qualified.
55 NamesAndTypesList columns_from_joined_table;
56 /// Columns will be added to block by JOIN. It's a subset of columns_from_joined_table with corrected Nullability
57 NamesAndTypesList columns_added_by_join;
58
59 /// Name -> original name. Names are the same as in columns_from_joined_table list.
60 std::unordered_map<String, String> original_names;
61 /// Original name -> name. Only ranamed columns.
62 std::unordered_map<String, String> renames;
63
64 String tmp_path;
65
66public:
67 AnalyzedJoin(const Settings &, const String & tmp_path);
68
69 /// for StorageJoin
70 AnalyzedJoin(SizeLimits limits, bool use_nulls, ASTTableJoin::Kind kind, ASTTableJoin::Strictness strictness,
71 const Names & key_names_right_)
72 : size_limits(limits)
73 , default_max_bytes(0)
74 , join_use_nulls(use_nulls)
75 , key_names_right(key_names_right_)
76 {
77 table_join.kind = kind;
78 table_join.strictness = strictness;
79 }
80
81 ASTTableJoin::Kind kind() const { return table_join.kind; }
82 ASTTableJoin::Strictness strictness() const { return table_join.strictness; }
83 const SizeLimits & sizeLimits() const { return size_limits; }
84 const String & getTemporaryPath() const { return tmp_path; }
85
86 bool forceNullableRight() const { return join_use_nulls && isLeftOrFull(table_join.kind); }
87 bool forceNullableLeft() const { return join_use_nulls && isRightOrFull(table_join.kind); }
88 size_t defaultMaxBytes() const { return default_max_bytes; }
89 size_t maxRowsInRightBlock() const { return partial_merge_join_rows_in_right_blocks; }
90 bool enablePartialMergeJoinOptimizations() const { return partial_merge_join_optimizations; }
91
92 void addUsingKey(const ASTPtr & ast);
93 void addOnKeys(ASTPtr & left_table_ast, ASTPtr & right_table_ast);
94
95 bool hasUsing() const { return table_join.using_expression_list != nullptr; }
96 bool hasOn() const { return table_join.on_expression != nullptr; }
97
98 NameSet getQualifiedColumnsSet() const;
99 NamesWithAliases getNamesWithAliases(const NameSet & required_columns) const;
100 NamesWithAliases getRequiredColumns(const Block & sample, const Names & action_columns) const;
101
102 void deduplicateAndQualifyColumnNames(const NameSet & left_table_columns, const String & right_table_prefix);
103 size_t rightKeyInclusion(const String & name) const;
104 NameSet requiredRightKeys() const;
105
106 void addJoinedColumn(const NameAndTypePair & joined_column);
107 void addJoinedColumnsAndCorrectNullability(Block & sample_block) const;
108
109 void setAsofInequality(ASOF::Inequality inequality) { asof_inequality = inequality; }
110 ASOF::Inequality getAsofInequality() { return asof_inequality; }
111
112 ASTPtr leftKeysList() const;
113 ASTPtr rightKeysList() const; /// For ON syntax only
114
115 Names requiredJoinedNames() const;
116 const Names & keyNamesLeft() const { return key_names_left; }
117 const Names & keyNamesRight() const { return key_names_right; }
118 const NamesAndTypesList & columnsFromJoinedTable() const { return columns_from_joined_table; }
119 const NamesAndTypesList & columnsAddedByJoin() const { return columns_added_by_join; }
120
121 /// StorageJoin overrides key names (cause of different names qualification)
122 void setRightKeys(const Names & keys) { key_names_right = keys; }
123
124 static bool sameJoin(const AnalyzedJoin * x, const AnalyzedJoin * y);
125 friend JoinPtr makeJoin(std::shared_ptr<AnalyzedJoin> table_join, const Block & right_sample_block);
126};
127
128bool isMergeJoin(const JoinPtr &);
129
130}
131