2022-07-20 15:25:24 +00:00
|
|
|
#include <Analyzer/TableFunctionNode.h>
|
|
|
|
|
|
|
|
#include <IO/WriteBuffer.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/Operators.h>
|
|
|
|
|
|
|
|
#include <Storages/IStorage.h>
|
|
|
|
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
TableFunctionNode::TableFunctionNode(String table_function_name_)
|
|
|
|
: table_function_name(table_function_name_)
|
|
|
|
, storage_id("system", "one")
|
|
|
|
{
|
|
|
|
children.resize(1);
|
|
|
|
children[arguments_child_index] = std::make_shared<ListNode>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableFunctionNode::resolve(TableFunctionPtr table_function_value, StoragePtr storage_value, ContextPtr context)
|
|
|
|
{
|
|
|
|
if (isResolved())
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Table function node {} already resolved", table_function_name);
|
|
|
|
|
|
|
|
table_function = std::move(table_function_value);
|
|
|
|
storage = std::move(storage_value);
|
|
|
|
storage_id = storage->getStorageID();
|
|
|
|
storage_snapshot = storage->getStorageSnapshot(storage->getInMemoryMetadataPtr(), context);
|
|
|
|
}
|
|
|
|
|
|
|
|
const StorageID & TableFunctionNode::getStorageID() const
|
|
|
|
{
|
|
|
|
if (!storage)
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Table function node {} is not resolved", table_function_name);
|
|
|
|
|
|
|
|
return storage_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
const StorageSnapshotPtr & TableFunctionNode::getStorageSnapshot() const
|
|
|
|
{
|
|
|
|
if (!storage)
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Table function node {} is not resolved", table_function_name);
|
|
|
|
|
|
|
|
return storage_snapshot;
|
|
|
|
}
|
|
|
|
|
|
|
|
String TableFunctionNode::getName() const
|
|
|
|
{
|
|
|
|
String name = table_function_name;
|
|
|
|
|
|
|
|
const auto & arguments = getArguments();
|
|
|
|
name += '(';
|
|
|
|
name += arguments.getName();
|
|
|
|
name += ')';
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableFunctionNode::dumpTreeImpl(WriteBuffer & buffer, FormatState & format_state, size_t indent) const
|
|
|
|
{
|
|
|
|
buffer << std::string(indent, ' ') << "TABLE_FUNCTION id: " << format_state.getNodeId(this);
|
|
|
|
|
|
|
|
if (hasAlias())
|
|
|
|
buffer << ", alias: " << getAlias();
|
|
|
|
|
|
|
|
buffer << ", table_function_name: " << table_function_name;
|
|
|
|
|
2022-09-04 15:20:59 +00:00
|
|
|
if (table_expression_modifiers)
|
|
|
|
{
|
|
|
|
buffer << ", ";
|
|
|
|
table_expression_modifiers->dump(buffer);
|
|
|
|
}
|
|
|
|
|
2022-07-20 15:25:24 +00:00
|
|
|
const auto & arguments = getArguments();
|
|
|
|
if (!arguments.getNodes().empty())
|
|
|
|
{
|
|
|
|
buffer << '\n' << std::string(indent + 2, ' ') << "ARGUMENTS\n";
|
|
|
|
arguments.dumpTreeImpl(buffer, format_state, indent + 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TableFunctionNode::isEqualImpl(const IQueryTreeNode & rhs) const
|
|
|
|
{
|
|
|
|
const auto & rhs_typed = assert_cast<const TableFunctionNode &>(rhs);
|
|
|
|
if (table_function_name != rhs_typed.table_function_name)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (storage && rhs_typed.storage)
|
|
|
|
return storage_id == rhs_typed.storage_id;
|
|
|
|
|
2022-09-04 15:20:59 +00:00
|
|
|
if (table_expression_modifiers && rhs_typed.table_expression_modifiers && table_expression_modifiers != rhs_typed.table_expression_modifiers)
|
|
|
|
return false;
|
|
|
|
else if (table_expression_modifiers && !rhs_typed.table_expression_modifiers)
|
|
|
|
return false;
|
|
|
|
else if (!table_expression_modifiers && rhs_typed.table_expression_modifiers)
|
|
|
|
return false;
|
|
|
|
|
2022-07-20 15:25:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableFunctionNode::updateTreeHashImpl(HashState & state) const
|
|
|
|
{
|
|
|
|
state.update(table_function_name.size());
|
|
|
|
state.update(table_function_name);
|
|
|
|
|
|
|
|
if (storage)
|
|
|
|
{
|
|
|
|
auto full_name = storage_id.getFullNameNotQuoted();
|
|
|
|
state.update(full_name.size());
|
|
|
|
state.update(full_name);
|
|
|
|
}
|
2022-09-04 15:20:59 +00:00
|
|
|
|
|
|
|
if (table_expression_modifiers)
|
|
|
|
table_expression_modifiers->updateTreeHash(state);
|
2022-07-20 15:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ASTPtr TableFunctionNode::toASTImpl() const
|
|
|
|
{
|
|
|
|
auto table_function_ast = std::make_shared<ASTFunction>();
|
|
|
|
|
|
|
|
table_function_ast->name = table_function_name;
|
|
|
|
|
|
|
|
const auto & arguments = getArguments();
|
|
|
|
if (!arguments.getNodes().empty())
|
|
|
|
{
|
|
|
|
table_function_ast->children.push_back(arguments.toAST());
|
|
|
|
table_function_ast->arguments = table_function_ast->children.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
return table_function_ast;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryTreeNodePtr TableFunctionNode::cloneImpl() const
|
|
|
|
{
|
|
|
|
auto result = std::make_shared<TableFunctionNode>(table_function_name);
|
|
|
|
|
|
|
|
result->storage = storage;
|
|
|
|
result->storage_id = storage_id;
|
|
|
|
result->storage_snapshot = storage_snapshot;
|
2022-09-04 15:20:59 +00:00
|
|
|
result->table_expression_modifiers = table_expression_modifiers;
|
2022-07-20 15:25:24 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|