2019-07-23 19:49:15 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-07-29 14:58:36 +00:00
|
|
|
#include <Core/Names.h>
|
2019-07-23 19:49:15 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Interpreters/InDepthNodeVisitor.h>
|
2019-07-24 15:37:37 +00:00
|
|
|
#include <Interpreters/Aliases.h>
|
2019-07-23 19:49:15 +00:00
|
|
|
|
2019-07-24 15:37:37 +00:00
|
|
|
|
2019-07-23 19:49:15 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-07-29 14:58:36 +00:00
|
|
|
class ASTIdentifier;
|
2019-09-03 14:36:02 +00:00
|
|
|
class AnalyzedJoin;
|
2019-07-23 19:49:15 +00:00
|
|
|
|
|
|
|
class CollectJoinOnKeysMatcher
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Visitor = ConstInDepthNodeVisitor<CollectJoinOnKeysMatcher, true>;
|
|
|
|
|
|
|
|
struct Data
|
|
|
|
{
|
|
|
|
AnalyzedJoin & analyzed_join;
|
2019-07-24 15:37:37 +00:00
|
|
|
const NameSet & source_columns;
|
|
|
|
const NameSet & joined_columns;
|
|
|
|
const Aliases & aliases;
|
2019-09-30 15:45:53 +00:00
|
|
|
const bool is_asof{false};
|
2019-07-29 14:58:36 +00:00
|
|
|
ASTPtr asof_left_key{};
|
|
|
|
ASTPtr asof_right_key{};
|
|
|
|
bool has_some{false};
|
|
|
|
|
|
|
|
void addJoinKeys(const ASTPtr & left_ast, const ASTPtr & right_ast, const std::pair<size_t, size_t> & table_no);
|
|
|
|
void addAsofJoinKeys(const ASTPtr & left_ast, const ASTPtr & right_ast, const std::pair<size_t, size_t> & table_no);
|
|
|
|
void asofToJoinKeys();
|
2019-07-23 19:49:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void visit(const ASTPtr & ast, Data & data)
|
|
|
|
{
|
|
|
|
if (auto * func = ast->as<ASTFunction>())
|
|
|
|
visit(*func, ast, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool needChildVisit(const ASTPtr & node, const ASTPtr &)
|
|
|
|
{
|
|
|
|
if (auto * func = node->as<ASTFunction>())
|
|
|
|
if (func->name == "equals")
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-07-29 14:58:36 +00:00
|
|
|
static void visit(const ASTFunction & func, const ASTPtr & ast, Data & data);
|
2019-07-23 19:49:15 +00:00
|
|
|
|
2019-07-29 14:58:36 +00:00
|
|
|
static void getIdentifiers(const ASTPtr & ast, std::vector<const ASTIdentifier *> & out);
|
|
|
|
static std::pair<size_t, size_t> getTableNumbers(const ASTPtr & expr, const ASTPtr & left_ast, const ASTPtr & right_ast, Data & data);
|
|
|
|
static const ASTIdentifier * unrollAliases(const ASTIdentifier * identifier, const Aliases & aliases);
|
|
|
|
static size_t getTableForIdentifiers(std::vector<const ASTIdentifier *> & identifiers, const Data & data);
|
2019-07-24 15:37:37 +00:00
|
|
|
|
2019-07-29 14:58:36 +00:00
|
|
|
[[noreturn]] static void throwSyntaxException(const String & msg);
|
2019-07-23 19:49:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Parse JOIN ON expression and collect ASTs for joined columns.
|
|
|
|
using CollectJoinOnKeysVisitor = CollectJoinOnKeysMatcher::Visitor;
|
|
|
|
|
|
|
|
}
|