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>
|
2022-12-07 17:23:14 +00:00
|
|
|
#include <Parsers/queryToString.h>
|
2019-07-23 19:49:15 +00:00
|
|
|
#include <Interpreters/InDepthNodeVisitor.h>
|
2019-12-24 18:51:37 +00:00
|
|
|
#include <Interpreters/DatabaseAndTableWithAlias.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;
|
2020-04-07 09:48:47 +00:00
|
|
|
class TableJoin;
|
2019-07-23 19:49:15 +00:00
|
|
|
|
2019-10-11 17:56:26 +00:00
|
|
|
namespace ASOF
|
|
|
|
{
|
|
|
|
enum class Inequality;
|
|
|
|
}
|
|
|
|
|
2022-12-07 17:23:14 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int INVALID_JOIN_ON_EXPRESSION;
|
|
|
|
}
|
|
|
|
|
2021-07-21 17:03:33 +00:00
|
|
|
enum class JoinIdentifierPos
|
|
|
|
{
|
|
|
|
/// Position can't be established, identifier not resolved
|
|
|
|
Unknown,
|
|
|
|
/// Left side of JOIN
|
|
|
|
Left,
|
|
|
|
/// Right side of JOIN
|
|
|
|
Right,
|
2022-12-15 13:07:07 +00:00
|
|
|
/// Identifier is not a column (e.g constant)
|
|
|
|
NotColumn,
|
2021-07-21 17:03:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
using JoinIdentifierPosPair = std::pair<JoinIdentifierPos, JoinIdentifierPos>;
|
|
|
|
|
|
|
|
|
2019-07-23 19:49:15 +00:00
|
|
|
class CollectJoinOnKeysMatcher
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Visitor = ConstInDepthNodeVisitor<CollectJoinOnKeysMatcher, true>;
|
|
|
|
|
|
|
|
struct Data
|
|
|
|
{
|
2020-04-07 09:48:47 +00:00
|
|
|
TableJoin & analyzed_join;
|
2020-06-05 21:17:00 +00:00
|
|
|
const TableWithColumnNamesAndTypes & left_table;
|
|
|
|
const TableWithColumnNamesAndTypes & right_table;
|
2019-07-24 15:37:37 +00:00
|
|
|
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{};
|
|
|
|
|
2023-08-23 15:46:56 +00:00
|
|
|
void addJoinKeys(const ASTPtr & left_ast, const ASTPtr & right_ast, JoinIdentifierPosPair table_pos, bool null_safe_comparison);
|
2021-07-21 17:03:33 +00:00
|
|
|
void addAsofJoinKeys(const ASTPtr & left_ast, const ASTPtr & right_ast, JoinIdentifierPosPair table_pos,
|
2022-07-29 16:30:50 +00:00
|
|
|
const ASOFJoinInequality & asof_inequality);
|
2019-07-29 14:58:36 +00:00
|
|
|
void asofToJoinKeys();
|
2019-07-23 19:49:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void visit(const ASTPtr & ast, Data & data)
|
|
|
|
{
|
|
|
|
if (auto * func = ast->as<ASTFunction>())
|
2021-07-21 17:03:33 +00:00
|
|
|
{
|
2019-07-23 19:49:15 +00:00
|
|
|
visit(*func, ast, data);
|
2021-07-21 17:03:33 +00:00
|
|
|
}
|
|
|
|
else if (auto * ident = ast->as<ASTIdentifier>())
|
|
|
|
{
|
|
|
|
visit(*ident, ast, data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-07 17:23:14 +00:00
|
|
|
if (ast->children.empty())
|
|
|
|
throw Exception(ErrorCodes::INVALID_JOIN_ON_EXPRESSION, "Illegal expression '{}' in JOIN ON section", queryToString(ast));
|
|
|
|
|
2021-07-21 17:03:33 +00:00
|
|
|
/// visit children
|
|
|
|
}
|
2019-07-23 19:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool needChildVisit(const ASTPtr & node, const ASTPtr &)
|
|
|
|
{
|
|
|
|
if (auto * func = node->as<ASTFunction>())
|
2020-08-05 09:31:59 +00:00
|
|
|
return func->name == "and";
|
2019-07-23 19:49:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-07-29 14:58:36 +00:00
|
|
|
static void visit(const ASTFunction & func, const ASTPtr & ast, Data & data);
|
2021-07-21 17:03:33 +00:00
|
|
|
static void visit(const ASTIdentifier & ident, 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);
|
2021-07-21 17:03:33 +00:00
|
|
|
static JoinIdentifierPosPair getTableNumbers(const ASTPtr & left_ast, const ASTPtr & right_ast, Data & data);
|
2019-07-29 14:58:36 +00:00
|
|
|
static const ASTIdentifier * unrollAliases(const ASTIdentifier * identifier, const Aliases & aliases);
|
2021-07-21 17:03:33 +00:00
|
|
|
static JoinIdentifierPos getTableForIdentifiers(const ASTPtr & ast, bool throw_on_table_mix, const Data & data);
|
2019-07-23 19:49:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Parse JOIN ON expression and collect ASTs for joined columns.
|
|
|
|
using CollectJoinOnKeysVisitor = CollectJoinOnKeysMatcher::Visitor;
|
|
|
|
|
|
|
|
}
|