2018-10-16 19:00:05 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-11-10 20:09:07 +00:00
|
|
|
#include <Parsers/IAST.h>
|
|
|
|
#include <Parsers/ASTSubquery.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTTablesInSelectQuery.h>
|
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Interpreters/ActionsVisitor.h>
|
|
|
|
#include <Interpreters/interpretSubquery.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
#include <Core/Block.h>
|
|
|
|
#include <Core/NamesAndTypes.h>
|
|
|
|
#include <Databases/IDatabase.h>
|
|
|
|
#include <Storages/StorageMemory.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2018-12-07 15:36:54 +00:00
|
|
|
#include <Interpreters/InDepthNodeVisitor.h>
|
2019-01-25 15:42:24 +00:00
|
|
|
#include <Interpreters/IdentifierSemantic.h>
|
2018-11-10 20:09:07 +00:00
|
|
|
|
2018-10-16 19:00:05 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-11-06 13:28:22 +00:00
|
|
|
|
2018-12-07 15:36:54 +00:00
|
|
|
class GlobalSubqueriesMatcher
|
2018-10-16 19:00:05 +00:00
|
|
|
{
|
|
|
|
public:
|
2018-12-07 15:36:54 +00:00
|
|
|
struct Data
|
2018-10-16 19:00:05 +00:00
|
|
|
{
|
2018-12-07 15:36:54 +00:00
|
|
|
const Context & context;
|
|
|
|
size_t subquery_depth;
|
|
|
|
bool is_remote;
|
|
|
|
size_t external_table_id;
|
|
|
|
Tables & external_tables;
|
|
|
|
SubqueriesForSets & subqueries_for_sets;
|
|
|
|
bool & has_global_subqueries;
|
|
|
|
|
|
|
|
Data(const Context & context_, size_t subquery_depth_, bool is_remote_,
|
|
|
|
Tables & tables, SubqueriesForSets & subqueries_for_sets_, bool & has_global_subqueries_)
|
|
|
|
: context(context_),
|
|
|
|
subquery_depth(subquery_depth_),
|
|
|
|
is_remote(is_remote_),
|
|
|
|
external_table_id(1),
|
|
|
|
external_tables(tables),
|
|
|
|
subqueries_for_sets(subqueries_for_sets_),
|
|
|
|
has_global_subqueries(has_global_subqueries_)
|
2018-10-17 10:59:05 +00:00
|
|
|
{}
|
2018-10-16 19:00:05 +00:00
|
|
|
|
2019-04-10 17:41:19 +00:00
|
|
|
void addExternalStorage(ASTPtr & ast, bool set_alias = false)
|
2018-10-16 19:00:05 +00:00
|
|
|
{
|
2018-12-07 15:36:54 +00:00
|
|
|
/// With nondistributed queries, creating temporary tables does not make sense.
|
|
|
|
if (!is_remote)
|
|
|
|
return;
|
2018-10-16 19:00:05 +00:00
|
|
|
|
2019-04-10 17:41:19 +00:00
|
|
|
bool is_table = false;
|
|
|
|
ASTPtr subquery_or_table_name = ast; /// ASTIdentifier | ASTSubquery | ASTTableExpression
|
2018-10-16 19:00:05 +00:00
|
|
|
|
2019-04-10 17:41:19 +00:00
|
|
|
if (const auto * ast_table_expr = ast->as<ASTTableExpression>())
|
2018-12-07 15:36:54 +00:00
|
|
|
{
|
2019-04-10 17:41:19 +00:00
|
|
|
subquery_or_table_name = ast_table_expr->subquery;
|
|
|
|
|
2018-12-07 15:36:54 +00:00
|
|
|
if (ast_table_expr->database_and_table_name)
|
|
|
|
{
|
2019-04-10 17:41:19 +00:00
|
|
|
subquery_or_table_name = ast_table_expr->database_and_table_name;
|
|
|
|
is_table = true;
|
2018-12-07 15:36:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-10 17:41:19 +00:00
|
|
|
else if (ast->as<ASTIdentifier>())
|
|
|
|
is_table = true;
|
2018-10-16 19:00:05 +00:00
|
|
|
|
2018-12-07 15:36:54 +00:00
|
|
|
if (!subquery_or_table_name)
|
|
|
|
throw Exception("Logical error: unknown AST element passed to ExpressionAnalyzer::addExternalStorage method",
|
|
|
|
ErrorCodes::LOGICAL_ERROR);
|
2018-10-16 19:00:05 +00:00
|
|
|
|
2019-04-10 17:41:19 +00:00
|
|
|
if (is_table)
|
2018-12-07 15:36:54 +00:00
|
|
|
{
|
|
|
|
/// If this is already an external table, you do not need to add anything. Just remember its presence.
|
2019-04-10 17:41:19 +00:00
|
|
|
if (external_tables.end() != external_tables.find(*getIdentifierName(subquery_or_table_name)))
|
2018-12-07 15:36:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-10-16 19:00:05 +00:00
|
|
|
|
2018-12-07 15:36:54 +00:00
|
|
|
/// Generate the name for the external table.
|
|
|
|
String external_table_name = "_data" + toString(external_table_id);
|
|
|
|
while (external_tables.count(external_table_name))
|
|
|
|
{
|
|
|
|
++external_table_id;
|
|
|
|
external_table_name = "_data" + toString(external_table_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto interpreter = interpretSubquery(subquery_or_table_name, context, subquery_depth, {});
|
|
|
|
|
|
|
|
Block sample = interpreter->getSampleBlock();
|
|
|
|
NamesAndTypesList columns = sample.getNamesAndTypesList();
|
2018-10-16 19:00:05 +00:00
|
|
|
|
2018-12-07 15:36:54 +00:00
|
|
|
StoragePtr external_storage = StorageMemory::create(external_table_name, ColumnsDescription{columns});
|
|
|
|
external_storage->startup();
|
2018-10-16 19:00:05 +00:00
|
|
|
|
2018-12-07 15:36:54 +00:00
|
|
|
/** We replace the subquery with the name of the temporary table.
|
|
|
|
* It is in this form, the request will go to the remote server.
|
|
|
|
* This temporary table will go to the remote server, and on its side,
|
|
|
|
* instead of doing a subquery, you just need to read it.
|
|
|
|
*/
|
2018-10-16 19:00:05 +00:00
|
|
|
|
2019-01-15 12:28:17 +00:00
|
|
|
auto database_and_table_name = createTableIdentifier("", external_table_name);
|
2019-04-10 17:41:19 +00:00
|
|
|
if (set_alias)
|
|
|
|
{
|
|
|
|
String alias = subquery_or_table_name->tryGetAlias();
|
|
|
|
if (auto * table_name = subquery_or_table_name->as<ASTIdentifier>())
|
|
|
|
if (alias.empty())
|
|
|
|
alias = table_name->shortName();
|
|
|
|
database_and_table_name->setAlias(alias);
|
|
|
|
}
|
2018-10-16 19:00:05 +00:00
|
|
|
|
2019-04-10 17:41:19 +00:00
|
|
|
if (auto * ast_table_expr = ast->as<ASTTableExpression>())
|
2018-12-07 15:36:54 +00:00
|
|
|
{
|
|
|
|
ast_table_expr->subquery.reset();
|
|
|
|
ast_table_expr->database_and_table_name = database_and_table_name;
|
|
|
|
|
|
|
|
ast_table_expr->children.clear();
|
|
|
|
ast_table_expr->children.emplace_back(database_and_table_name);
|
|
|
|
}
|
|
|
|
else
|
2019-04-10 17:41:19 +00:00
|
|
|
ast = database_and_table_name;
|
2018-12-07 15:36:54 +00:00
|
|
|
|
|
|
|
external_tables[external_table_name] = external_storage;
|
|
|
|
subqueries_for_sets[external_table_name].source = interpreter->execute().in;
|
|
|
|
subqueries_for_sets[external_table_name].table = external_storage;
|
|
|
|
|
|
|
|
/** NOTE If it was written IN tmp_table - the existing temporary (but not external) table,
|
|
|
|
* then a new temporary table will be created (for example, _data1),
|
|
|
|
* and the data will then be copied to it.
|
|
|
|
* Maybe this can be avoided.
|
2018-10-16 19:00:05 +00:00
|
|
|
*/
|
2018-12-07 15:36:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-22 13:33:56 +00:00
|
|
|
static void visit(ASTPtr & ast, Data & data)
|
2018-12-07 15:36:54 +00:00
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
if (auto * t = ast->as<ASTFunction>())
|
2018-12-07 15:36:54 +00:00
|
|
|
visit(*t, ast, data);
|
2019-03-11 13:22:51 +00:00
|
|
|
if (auto * t = ast->as<ASTTablesInSelectQueryElement>())
|
2018-12-07 15:36:54 +00:00
|
|
|
visit(*t, ast, data);
|
|
|
|
}
|
2018-10-16 19:00:05 +00:00
|
|
|
|
2018-12-07 15:36:54 +00:00
|
|
|
static bool needChildVisit(ASTPtr &, const ASTPtr & child)
|
|
|
|
{
|
|
|
|
/// We do not go into subqueries.
|
2019-03-11 13:22:51 +00:00
|
|
|
if (child->as<ASTSelectQuery>())
|
2018-12-07 15:36:54 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-16 19:00:05 +00:00
|
|
|
|
2018-12-07 15:36:54 +00:00
|
|
|
private:
|
|
|
|
/// GLOBAL IN
|
|
|
|
static void visit(ASTFunction & func, ASTPtr &, Data & data)
|
|
|
|
{
|
|
|
|
if (func.name == "globalIn" || func.name == "globalNotIn")
|
2018-10-16 19:00:05 +00:00
|
|
|
{
|
2018-12-07 15:36:54 +00:00
|
|
|
data.addExternalStorage(func.arguments->children[1]);
|
|
|
|
data.has_global_subqueries = true;
|
|
|
|
}
|
|
|
|
}
|
2018-10-16 19:00:05 +00:00
|
|
|
|
2018-12-07 15:36:54 +00:00
|
|
|
/// GLOBAL JOIN
|
|
|
|
static void visit(ASTTablesInSelectQueryElement & table_elem, ASTPtr &, Data & data)
|
|
|
|
{
|
2019-03-15 16:14:13 +00:00
|
|
|
if (table_elem.table_join && table_elem.table_join->as<ASTTableJoin &>().locality == ASTTableJoin::Locality::Global)
|
2018-12-07 15:36:54 +00:00
|
|
|
{
|
2019-04-10 17:41:19 +00:00
|
|
|
data.addExternalStorage(table_elem.table_expression, true);
|
2018-12-07 15:36:54 +00:00
|
|
|
data.has_global_subqueries = true;
|
2018-10-16 19:00:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-07 15:36:54 +00:00
|
|
|
/// Converts GLOBAL subqueries to external tables; Puts them into the external_tables dictionary: name -> StoragePtr.
|
|
|
|
using GlobalSubqueriesVisitor = InDepthNodeVisitor<GlobalSubqueriesMatcher, false>;
|
|
|
|
|
2018-10-16 19:00:05 +00:00
|
|
|
}
|