ClickHouse/src/Interpreters/CollectJoinOnKeysVisitor.h
Vladimir 576b407804
Support conditions in JOIN ON section (#24420)
* Try to enforce table identification in CollectJoinOnKeysMatcher

* Support filtering conditions in JOIN ON for HashJoin

* Correct handle non equi join

* Update test 00878_join_unexpected_results

* Join on filters calculated as one row before join

* Do not lookup key in hash join if condition for row is not hold

* better

* Support filtering conditions in JOIN ON for MergeJoin

* Support Nullable mask in JOIN ON section

* Fix style in Interpreters/TableJoin.cpp

* Change return type of getColumnAsMask in join_common to ColumnPtr

* Handle Nullable(Nothing) type in JOIN ON section, add test cases

* Fix type cast JoinCommon::getColumnAsMask

* Check type if conditions in JOIN ON section, support functions

* Update tests with JOIN ON

* Style changes, add comments for conditions in JOIN ON section

* Add test cases for join on condtions

* JOIN ON key1 = key2 AND (cond1 OR cond2)

* Remove CollectJoinOnKeysVisitor has_join_keys

* Add test cases for join on nullable/lc conditions

* Fix style

* Change error code 48 to 403 in join on tests

* Fix whitespace
2021-07-21 20:03:33 +03:00

94 lines
2.6 KiB
C++

#pragma once
#include <Core/Names.h>
#include <Parsers/ASTFunction.h>
#include <Interpreters/InDepthNodeVisitor.h>
#include <Interpreters/DatabaseAndTableWithAlias.h>
#include <Interpreters/Aliases.h>
namespace DB
{
class ASTIdentifier;
class TableJoin;
namespace ASOF
{
enum class Inequality;
}
enum class JoinIdentifierPos
{
/// Position can't be established, identifier not resolved
Unknown,
/// Left side of JOIN
Left,
/// Right side of JOIN
Right,
/// Expression not valid, e.g. doesn't contain identifiers
NotApplicable,
};
using JoinIdentifierPosPair = std::pair<JoinIdentifierPos, JoinIdentifierPos>;
class CollectJoinOnKeysMatcher
{
public:
using Visitor = ConstInDepthNodeVisitor<CollectJoinOnKeysMatcher, true>;
struct Data
{
TableJoin & analyzed_join;
const TableWithColumnNamesAndTypes & left_table;
const TableWithColumnNamesAndTypes & right_table;
const Aliases & aliases;
const bool is_asof{false};
ASTPtr asof_left_key{};
ASTPtr asof_right_key{};
void addJoinKeys(const ASTPtr & left_ast, const ASTPtr & right_ast, JoinIdentifierPosPair table_pos);
void addAsofJoinKeys(const ASTPtr & left_ast, const ASTPtr & right_ast, JoinIdentifierPosPair table_pos,
const ASOF::Inequality & asof_inequality);
void asofToJoinKeys();
};
static void visit(const ASTPtr & ast, Data & data)
{
if (auto * func = ast->as<ASTFunction>())
{
visit(*func, ast, data);
}
else if (auto * ident = ast->as<ASTIdentifier>())
{
visit(*ident, ast, data);
}
else
{
/// visit children
}
}
static bool needChildVisit(const ASTPtr & node, const ASTPtr &)
{
if (auto * func = node->as<ASTFunction>())
return func->name == "and";
return true;
}
private:
static void visit(const ASTFunction & func, const ASTPtr & ast, Data & data);
static void visit(const ASTIdentifier & ident, const ASTPtr & ast, Data & data);
static void getIdentifiers(const ASTPtr & ast, std::vector<const ASTIdentifier *> & out);
static JoinIdentifierPosPair getTableNumbers(const ASTPtr & left_ast, const ASTPtr & right_ast, Data & data);
static const ASTIdentifier * unrollAliases(const ASTIdentifier * identifier, const Aliases & aliases);
static JoinIdentifierPos getTableForIdentifiers(const ASTPtr & ast, bool throw_on_table_mix, const Data & data);
};
/// Parse JOIN ON expression and collect ASTs for joined columns.
using CollectJoinOnKeysVisitor = CollectJoinOnKeysMatcher::Visitor;
}