2022-07-18 15:53:30 +00:00
|
|
|
#include <Interpreters/PreparedSets.h>
|
|
|
|
#include <Processors/QueryPlan/QueryPlan.h>
|
2022-07-19 10:28:21 +00:00
|
|
|
#include <Interpreters/InterpreterSelectWithUnionQuery.h>
|
2022-07-18 15:53:30 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
PreparedSetKey PreparedSetKey::forLiteral(const IAST & ast, DataTypes types_)
|
|
|
|
{
|
|
|
|
/// Remove LowCardinality types from type list because Set doesn't support LowCardinality keys now,
|
|
|
|
/// just converts LowCardinality to ordinary types.
|
|
|
|
for (auto & type : types_)
|
|
|
|
type = recursiveRemoveLowCardinality(type);
|
|
|
|
|
|
|
|
PreparedSetKey key;
|
|
|
|
key.ast_hash = ast.getTreeHash();
|
|
|
|
key.types = std::move(types_);
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
PreparedSetKey PreparedSetKey::forSubquery(const IAST & ast)
|
|
|
|
{
|
|
|
|
PreparedSetKey key;
|
|
|
|
key.ast_hash = ast.getTreeHash();
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PreparedSetKey::operator==(const PreparedSetKey & other) const
|
|
|
|
{
|
|
|
|
if (ast_hash != other.ast_hash)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (types.size() != other.types.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < types.size(); ++i)
|
|
|
|
{
|
|
|
|
if (!types[i]->equals(*other.types[i]))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-19 10:28:21 +00:00
|
|
|
SubqueryForSet & PreparedSets::createOrGetSubquery(const String & subquery_id, const PreparedSetKey & key, SetPtr set_)
|
2022-07-18 15:53:30 +00:00
|
|
|
{
|
2022-07-19 10:28:21 +00:00
|
|
|
SubqueryForSet & subquery = subqueries[subquery_id];
|
|
|
|
|
|
|
|
/// If you already created a Set with the same subquery / table for another ast
|
|
|
|
/// In that case several PreparedSetKey would share same subquery and set
|
|
|
|
/// Not sure if it's really possible case (maybe for distributed query when set was filled by external table?)
|
|
|
|
if (subquery.set)
|
|
|
|
sets[key] = subquery.set;
|
|
|
|
else
|
|
|
|
sets[key] = subquery.set = set_;
|
|
|
|
return subquery;
|
2022-07-18 15:53:30 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 10:28:21 +00:00
|
|
|
SubqueryForSet & PreparedSets::getSubquery(const String & subquery_id) { return subqueries[subquery_id]; }
|
|
|
|
|
|
|
|
void PreparedSets::setSet(const PreparedSetKey & key, SetPtr set_) { sets[key] = set_; }
|
2022-07-18 15:53:30 +00:00
|
|
|
|
2022-07-19 10:28:21 +00:00
|
|
|
SetPtr & PreparedSets::getSet(const PreparedSetKey & key) { return sets[key]; }
|
|
|
|
|
|
|
|
std::vector<SetPtr> PreparedSets::getByTreeHash(IAST::Hash ast_hash)
|
2022-07-18 15:53:30 +00:00
|
|
|
{
|
2022-07-19 10:28:21 +00:00
|
|
|
std::vector<SetPtr> res;
|
|
|
|
for (const auto & it : this->sets)
|
|
|
|
{
|
|
|
|
if (it.first.ast_hash == ast_hash)
|
|
|
|
res.push_back(it.second);
|
|
|
|
}
|
|
|
|
return res;
|
2022-07-18 15:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PreparedSets::SubqueriesForSets PreparedSets::moveSubqueries()
|
|
|
|
{
|
|
|
|
auto res = std::move(subqueries);
|
|
|
|
subqueries = SubqueriesForSets();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-07-19 10:28:21 +00:00
|
|
|
bool PreparedSets::empty() const { return sets.empty(); }
|
2022-07-18 15:53:30 +00:00
|
|
|
|
2022-07-19 10:28:21 +00:00
|
|
|
SubqueryForSet::SubqueryForSet() = default;
|
2022-07-18 15:53:30 +00:00
|
|
|
|
|
|
|
SubqueryForSet::~SubqueryForSet() = default;
|
|
|
|
|
|
|
|
SubqueryForSet::SubqueryForSet(SubqueryForSet &&) noexcept = default;
|
|
|
|
|
2022-07-19 10:28:21 +00:00
|
|
|
void SubqueryForSet::setSource(InterpreterSelectWithUnionQuery & interpreter, StoragePtr table_)
|
|
|
|
{
|
|
|
|
source = std::make_unique<QueryPlan>();
|
|
|
|
interpreter.buildQueryPlan(*source);
|
|
|
|
table = table_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SubqueryForSet::hasSource() const
|
|
|
|
{
|
|
|
|
return source != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryPlanPtr SubqueryForSet::moveSource()
|
|
|
|
{
|
|
|
|
auto res = std::move(source);
|
|
|
|
source = nullptr;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-07-18 15:53:30 +00:00
|
|
|
};
|