ClickHouse/dbms/src/Interpreters/AnalyzedJoin.h

75 lines
2.7 KiB
C++
Raw Normal View History

2018-11-02 18:53:23 +00:00
#pragma once
#include <Core/Names.h>
#include <Core/NamesAndTypes.h>
#include <Parsers/IAST.h>
#include <utility>
#include <memory>
namespace DB
{
class Context;
class ASTSelectQuery;
2019-02-13 19:00:52 +00:00
struct DatabaseAndTableWithAlias;
2018-11-02 18:53:23 +00:00
class ExpressionActions;
using ExpressionActionsPtr = std::shared_ptr<ExpressionActions>;
struct AnalyzedJoin
{
/** 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
private:
friend class SyntaxAnalyzer;
friend class ExpressionAnalyzer;
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;
bool with_using = true;
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;
/// Columns from joined table which may be added to block. It's columns_from_joined_table with possibly modified types.
NamesAndTypesList available_joined_columns;
/// 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:
void addUsingKey(const ASTPtr & ast);
void addOnKeys(ASTPtr & left_table_ast, ASTPtr & right_table_ast);
2019-02-06 16:44:47 +00:00
2018-11-08 09:00:25 +00:00
ExpressionActionsPtr createJoinedBlockActions(
2019-07-30 18:39:37 +00:00
const NamesAndTypesList & columns_added_by_join, /// Subset of available_joined_columns.
2018-11-08 09:00:25 +00:00
const ASTSelectQuery * select_query_with_join,
const Context & context) const;
2019-07-30 18:39:37 +00:00
NameSet getQualifiedColumnsSet() const;
NameSet getOriginalColumnsSet() const;
2019-08-01 17:27:51 +00:00
Names getOriginalColumnNames(const Names & required_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-02-13 19:00:52 +00:00
void calculateAvailableJoinedColumns(bool make_nullable);
size_t rightKeyInclusion(const String & name) const;
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);
}