2022-09-05 10:24:54 +00:00
|
|
|
#include <Planner/CollectSets.h>
|
2022-08-27 13:16:02 +00:00
|
|
|
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
|
2022-08-27 14:32:50 +00:00
|
|
|
#include <Storages/StorageSet.h>
|
|
|
|
|
2022-08-27 13:16:02 +00:00
|
|
|
#include <Analyzer/Utils.h>
|
|
|
|
#include <Analyzer/SetUtils.h>
|
|
|
|
#include <Analyzer/InDepthQueryTreeVisitor.h>
|
|
|
|
#include <Analyzer/ConstantNode.h>
|
|
|
|
#include <Analyzer/FunctionNode.h>
|
2022-08-27 14:32:50 +00:00
|
|
|
#include <Analyzer/TableNode.h>
|
2022-08-27 13:16:02 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-09-06 15:25:52 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int UNSUPPORTED_METHOD;
|
|
|
|
}
|
|
|
|
|
2022-08-27 13:16:02 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2022-10-07 13:20:45 +00:00
|
|
|
class CollectSetsVisitor : public ConstInDepthQueryTreeVisitor<CollectSetsVisitor>
|
2022-08-27 13:16:02 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-10-17 15:09:45 +00:00
|
|
|
explicit CollectSetsVisitor(PlannerContext & planner_context_)
|
2022-10-07 13:20:45 +00:00
|
|
|
: planner_context(planner_context_)
|
|
|
|
{}
|
2022-08-27 13:16:02 +00:00
|
|
|
|
2022-10-07 13:20:45 +00:00
|
|
|
void visitImpl(const QueryTreeNodePtr & node)
|
2022-08-27 13:16:02 +00:00
|
|
|
{
|
|
|
|
auto * function_node = node->as<FunctionNode>();
|
|
|
|
if (!function_node || !isNameOfInFunction(function_node->getFunctionName()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto in_first_argument = function_node->getArguments().getNodes().at(0);
|
|
|
|
auto in_second_argument = function_node->getArguments().getNodes().at(1);
|
|
|
|
auto in_second_argument_node_type = in_second_argument->getNodeType();
|
|
|
|
|
|
|
|
const auto & settings = planner_context.getQueryContext()->getSettingsRef();
|
|
|
|
|
2022-10-17 15:09:45 +00:00
|
|
|
String set_key = planner_context.createSetKey(in_second_argument);
|
2022-08-27 13:16:02 +00:00
|
|
|
|
2022-10-17 15:09:45 +00:00
|
|
|
if (planner_context.hasSet(set_key))
|
2022-08-27 13:16:02 +00:00
|
|
|
return;
|
|
|
|
|
2022-08-27 14:32:50 +00:00
|
|
|
/// Tables and table functions are replaced with subquery at Analysis stage, except special Set table.
|
|
|
|
auto * second_argument_table = in_second_argument->as<TableNode>();
|
|
|
|
StorageSet * storage_set = second_argument_table != nullptr ? dynamic_cast<StorageSet *>(second_argument_table->getStorage().get()) : nullptr;
|
|
|
|
|
|
|
|
if (storage_set)
|
|
|
|
{
|
2022-10-17 15:09:45 +00:00
|
|
|
planner_context.registerSet(set_key, PlannerSet(storage_set->getSet()));
|
2022-08-27 14:32:50 +00:00
|
|
|
}
|
2022-11-29 11:35:05 +00:00
|
|
|
else if (const auto * constant_node = in_second_argument->as<ConstantNode>())
|
2022-08-31 15:21:17 +00:00
|
|
|
{
|
|
|
|
auto set = makeSetForConstantValue(
|
|
|
|
in_first_argument->getResultType(),
|
2022-11-29 11:35:05 +00:00
|
|
|
constant_node->getValue(),
|
|
|
|
constant_node->getResultType(),
|
2022-08-31 15:21:17 +00:00
|
|
|
settings);
|
|
|
|
|
2022-10-17 15:09:45 +00:00
|
|
|
planner_context.registerSet(set_key, PlannerSet(std::move(set)));
|
2022-08-31 15:21:17 +00:00
|
|
|
}
|
2022-08-27 14:32:50 +00:00
|
|
|
else if (in_second_argument_node_type == QueryTreeNodeType::QUERY ||
|
2022-08-27 13:16:02 +00:00
|
|
|
in_second_argument_node_type == QueryTreeNodeType::UNION)
|
|
|
|
{
|
|
|
|
SizeLimits size_limits_for_set = {settings.max_rows_in_set, settings.max_bytes_in_set, settings.set_overflow_mode};
|
|
|
|
bool tranform_null_in = settings.transform_null_in;
|
|
|
|
auto set = std::make_shared<Set>(size_limits_for_set, false /*fill_set_elements*/, tranform_null_in);
|
|
|
|
|
2022-10-17 15:09:45 +00:00
|
|
|
planner_context.registerSet(set_key, PlannerSet(std::move(set), in_second_argument));
|
2022-08-27 13:16:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw Exception(ErrorCodes::UNSUPPORTED_METHOD,
|
2022-10-06 19:36:06 +00:00
|
|
|
"Function '{}' is supported only if second argument is constant or table expression",
|
2022-09-06 15:25:52 +00:00
|
|
|
function_node->getFunctionName());
|
2022-08-27 13:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 14:41:24 +00:00
|
|
|
static bool needChildVisit(const QueryTreeNodePtr & parent_node, const QueryTreeNodePtr & child_node)
|
2022-08-27 13:16:02 +00:00
|
|
|
{
|
2023-02-08 14:41:24 +00:00
|
|
|
if (parent_node->getNodeType() == QueryTreeNodeType::CONSTANT)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto child_node_type = child_node->getNodeType();
|
|
|
|
return !(child_node_type == QueryTreeNodeType::QUERY || child_node_type == QueryTreeNodeType::UNION);
|
2022-08-27 13:16:02 +00:00
|
|
|
}
|
|
|
|
|
2022-10-07 13:20:45 +00:00
|
|
|
private:
|
2022-10-17 15:09:45 +00:00
|
|
|
PlannerContext & planner_context;
|
2022-10-07 13:20:45 +00:00
|
|
|
};
|
2022-08-27 13:16:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-10-17 15:09:45 +00:00
|
|
|
void collectSets(const QueryTreeNodePtr & node, PlannerContext & planner_context)
|
2022-08-27 13:16:02 +00:00
|
|
|
{
|
2022-10-07 13:20:45 +00:00
|
|
|
CollectSetsVisitor visitor(planner_context);
|
2022-08-27 13:16:02 +00:00
|
|
|
visitor.visit(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|