2015-09-18 13:36:10 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Interpreters/Context_fwd.h>
|
2019-04-11 19:29:28 +00:00
|
|
|
#include <Parsers/IAST_fwd.h>
|
2019-05-17 14:34:25 +00:00
|
|
|
#include <Storages/IStorage_fwd.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <common/types.h>
|
2019-05-17 14:34:25 +00:00
|
|
|
|
|
|
|
#include <memory>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <string>
|
2015-09-18 13:36:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-01-03 07:37:29 +00:00
|
|
|
class ASTSelectQuery;
|
2015-09-18 13:36:10 +00:00
|
|
|
|
2017-01-04 02:37:47 +00:00
|
|
|
/** Scheme of operation:
|
|
|
|
*
|
|
|
|
* If "main" table in a query is distributed enough (thus, have at least two shards),
|
|
|
|
* and there are non-GLOBAL subqueries in IN or JOIN,
|
|
|
|
* and in that subqueries, there is a table
|
|
|
|
* (in any nesting level in that subquery or in more deep subqueries),
|
|
|
|
* that exist on local server and (according to information on local server) is also distributed enough
|
|
|
|
* then, according to setting 'distributed_product_mode',
|
|
|
|
* either
|
|
|
|
* - throw an exception;
|
|
|
|
* - or add GLOBAL to top subquery;
|
|
|
|
* - or replace database and table name in subquery to remote database and table name,
|
|
|
|
* as Distributed storage on local server knows it.
|
|
|
|
*
|
|
|
|
* Do not recursively preprocess subqueries, as it will be done by calling code.
|
|
|
|
*/
|
2015-09-18 13:36:10 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
class InJoinSubqueriesPreprocessor : WithContext
|
2015-09-18 13:36:10 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-03-27 20:12:14 +00:00
|
|
|
using SubqueryTables = std::vector<std::pair<ASTPtr, std::vector<ASTPtr>>>; /// {subquery, renamed_tables}
|
|
|
|
|
2019-04-11 19:29:28 +00:00
|
|
|
struct CheckShardsAndTables
|
|
|
|
{
|
|
|
|
using Ptr = std::unique_ptr<CheckShardsAndTables>;
|
2015-09-18 13:36:10 +00:00
|
|
|
|
2020-08-08 01:01:47 +00:00
|
|
|
/// These methods could be overridden for the need of the unit test.
|
2019-04-11 19:29:28 +00:00
|
|
|
virtual bool hasAtLeastTwoShards(const IStorage & table) const;
|
|
|
|
virtual std::pair<std::string, std::string> getRemoteDatabaseAndTableName(const IStorage & table) const;
|
|
|
|
virtual ~CheckShardsAndTables() {}
|
|
|
|
};
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
InJoinSubqueriesPreprocessor(
|
|
|
|
ContextPtr context_,
|
|
|
|
SubqueryTables & renamed_tables_,
|
|
|
|
CheckShardsAndTables::Ptr _checker = std::make_unique<CheckShardsAndTables>())
|
|
|
|
: WithContext(context_), renamed_tables(renamed_tables_), checker(std::move(_checker))
|
|
|
|
{
|
|
|
|
}
|
2019-04-11 19:29:28 +00:00
|
|
|
|
2020-03-09 00:08:02 +00:00
|
|
|
void visit(ASTPtr & ast) const;
|
2015-09-18 13:36:10 +00:00
|
|
|
|
|
|
|
private:
|
2020-03-27 20:12:14 +00:00
|
|
|
SubqueryTables & renamed_tables;
|
2019-04-11 19:29:28 +00:00
|
|
|
CheckShardsAndTables::Ptr checker;
|
2015-09-18 13:36:10 +00:00
|
|
|
};
|
|
|
|
|
2017-01-03 07:37:29 +00:00
|
|
|
|
2015-09-18 13:36:10 +00:00
|
|
|
}
|