ClickHouse/dbms/src/Interpreters/AnalyzedJoin.h

84 lines
3.1 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;
class ExpressionActions;
using ExpressionActionsPtr = std::shared_ptr<ExpressionActions>;
struct JoinedColumn
{
/// Column will be joined to block.
NameAndTypePair name_and_type;
/// original column name from joined source.
String original_name;
JoinedColumn(NameAndTypePair name_and_type_, String original_name_)
: name_and_type(std::move(name_and_type_)), original_name(std::move(original_name_)) {}
bool operator==(const JoinedColumn & o) const
{
return name_and_type == o.name_and_type && original_name == o.original_name;
}
};
using JoinedColumnsList = std::list<JoinedColumn>;
struct AnalyzedJoin
{
/// NOTE: So far, only one JOIN per query is supported.
/** 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)`.
*/
Names key_names_left;
Names key_names_right; /// Duplicating names are qualified.
ASTs key_asts_left;
ASTs key_asts_right;
/// All columns which can be read from joined table. Duplicating names are qualified.
JoinedColumnsList columns_from_joined_table;
2018-11-08 09:00:25 +00:00
/// Columns from joined table which may be added to block.
/// It's columns_from_joined_table without duplicate columns and possibly modified types.
2018-11-07 12:55:19 +00:00
JoinedColumnsList available_joined_columns;
2018-11-08 09:00:25 +00:00
ExpressionActionsPtr createJoinedBlockActions(
const JoinedColumnsList & columns_added_by_join, /// Subset of available_joined_columns.
const ASTSelectQuery * select_query_with_join,
const Context & context) const;
Names getOriginalColumnNames(const NameSet & required_columns) const;
2018-11-02 18:53:23 +00:00
const JoinedColumnsList & getColumnsFromJoinedTable(const NameSet & source_columns,
const Context & context,
const ASTSelectQuery * select_query_with_join);
void calculateAvailableJoinedColumns(const NameSet & source_columns,
const Context & context,
const ASTSelectQuery * select_query_with_join,
bool make_nullable);
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);
}