2022-07-14 11:20:16 +00:00
|
|
|
#include <Analyzer/ColumnNode.h>
|
|
|
|
|
|
|
|
#include <Common/SipHash.h>
|
|
|
|
|
|
|
|
#include <IO/WriteBuffer.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/Operators.h>
|
|
|
|
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
|
2023-01-26 10:52:40 +00:00
|
|
|
#include <Analyzer/TableNode.h>
|
|
|
|
|
2022-07-14 11:20:16 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:25:58 +00:00
|
|
|
ColumnNode::ColumnNode(NameAndTypePair column_, QueryTreeNodePtr expression_node_, QueryTreeNodeWeakPtr column_source_)
|
|
|
|
: IQueryTreeNode(children_size, weak_pointers_size)
|
2022-10-07 10:44:28 +00:00
|
|
|
, column(std::move(column_))
|
2022-07-20 10:34:29 +00:00
|
|
|
{
|
2022-10-10 10:25:58 +00:00
|
|
|
children[expression_child_index] = std::move(expression_node_);
|
|
|
|
getSourceWeakPointer() = std::move(column_source_);
|
2022-07-20 10:34:29 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 10:25:58 +00:00
|
|
|
ColumnNode::ColumnNode(NameAndTypePair column_, QueryTreeNodeWeakPtr column_source_)
|
|
|
|
: ColumnNode(std::move(column_), nullptr /*expression_node*/, std::move(column_source_))
|
2022-07-20 10:34:29 +00:00
|
|
|
{
|
2022-08-15 16:34:10 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 11:20:16 +00:00
|
|
|
QueryTreeNodePtr ColumnNode::getColumnSource() const
|
|
|
|
{
|
2022-10-10 10:25:58 +00:00
|
|
|
auto lock = getSourceWeakPointer().lock();
|
2022-07-14 11:20:16 +00:00
|
|
|
if (!lock)
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR,
|
|
|
|
"Column {} {} query tree node does not have valid source node",
|
|
|
|
column.name,
|
|
|
|
column.type->getName());
|
|
|
|
|
|
|
|
return lock;
|
|
|
|
}
|
|
|
|
|
2022-09-03 12:11:09 +00:00
|
|
|
QueryTreeNodePtr ColumnNode::getColumnSourceOrNull() const
|
|
|
|
{
|
2022-10-10 10:25:58 +00:00
|
|
|
return getSourceWeakPointer().lock();
|
2022-09-03 12:11:09 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 10:54:45 +00:00
|
|
|
void ColumnNode::dumpTreeImpl(WriteBuffer & buffer, FormatState & state, size_t indent) const
|
2022-07-14 11:20:16 +00:00
|
|
|
{
|
2022-07-19 10:54:45 +00:00
|
|
|
buffer << std::string(indent, ' ') << "COLUMN id: " << state.getNodeId(this);
|
|
|
|
|
|
|
|
if (hasAlias())
|
|
|
|
buffer << ", alias: " << getAlias();
|
|
|
|
|
|
|
|
buffer << ", column_name: " << column.name << ", result_type: " << column.type->getName();
|
|
|
|
|
2022-10-10 10:25:58 +00:00
|
|
|
auto column_source_ptr = getSourceWeakPointer().lock();
|
2022-07-19 10:54:45 +00:00
|
|
|
if (column_source_ptr)
|
|
|
|
buffer << ", source_id: " << state.getNodeId(column_source_ptr.get());
|
2022-07-20 10:34:29 +00:00
|
|
|
|
2022-08-15 16:34:10 +00:00
|
|
|
const auto & expression = getExpression();
|
2022-07-20 10:34:29 +00:00
|
|
|
|
2022-08-15 16:34:10 +00:00
|
|
|
if (expression)
|
2022-07-20 10:34:29 +00:00
|
|
|
{
|
2022-08-15 16:34:10 +00:00
|
|
|
buffer << '\n' << std::string(indent + 2, ' ') << "EXPRESSION\n";
|
|
|
|
expression->dumpTreeImpl(buffer, state, indent + 4);
|
2022-07-20 10:34:29 +00:00
|
|
|
}
|
2022-07-14 11:20:16 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 13:32:53 +00:00
|
|
|
bool ColumnNode::isEqualImpl(const IQueryTreeNode & rhs) const
|
|
|
|
{
|
|
|
|
const auto & rhs_typed = assert_cast<const ColumnNode &>(rhs);
|
2023-01-26 17:16:36 +00:00
|
|
|
return column == rhs_typed.column;
|
2022-07-15 13:32:53 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 11:20:16 +00:00
|
|
|
void ColumnNode::updateTreeHashImpl(HashState & hash_state) const
|
|
|
|
{
|
|
|
|
hash_state.update(column.name.size());
|
|
|
|
hash_state.update(column.name);
|
|
|
|
|
2022-07-14 12:06:25 +00:00
|
|
|
const auto & column_type_name = column.type->getName();
|
|
|
|
hash_state.update(column_type_name.size());
|
|
|
|
hash_state.update(column_type_name);
|
2022-07-14 11:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QueryTreeNodePtr ColumnNode::cloneImpl() const
|
|
|
|
{
|
2023-02-08 14:41:24 +00:00
|
|
|
return std::make_shared<ColumnNode>(column, getSourceWeakPointer());
|
2022-07-14 11:20:16 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 09:14:58 +00:00
|
|
|
ASTPtr ColumnNode::toASTImpl(const ConvertToASTOptions & options) const
|
2022-07-14 11:20:16 +00:00
|
|
|
{
|
2023-02-05 12:19:21 +00:00
|
|
|
std::vector<std::string> column_identifier_parts;
|
2023-01-26 10:52:40 +00:00
|
|
|
|
|
|
|
auto column_source = getColumnSourceOrNull();
|
2023-03-07 20:39:26 +00:00
|
|
|
if (column_source && options.fully_qualified_identifiers)
|
2023-01-26 10:52:40 +00:00
|
|
|
{
|
|
|
|
auto node_type = column_source->getNodeType();
|
|
|
|
if (node_type == QueryTreeNodeType::TABLE ||
|
|
|
|
node_type == QueryTreeNodeType::TABLE_FUNCTION ||
|
|
|
|
node_type == QueryTreeNodeType::QUERY ||
|
|
|
|
node_type == QueryTreeNodeType::UNION)
|
|
|
|
{
|
|
|
|
if (column_source->hasAlias())
|
|
|
|
{
|
2023-02-05 12:19:21 +00:00
|
|
|
column_identifier_parts = {column_source->getAlias()};
|
2023-01-26 10:52:40 +00:00
|
|
|
}
|
|
|
|
else if (auto * table_node = column_source->as<TableNode>())
|
|
|
|
{
|
2023-02-25 19:16:51 +00:00
|
|
|
if (!table_node->getTemporaryTableName().empty())
|
|
|
|
{
|
|
|
|
column_identifier_parts = { table_node->getTemporaryTableName() };
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const auto & table_storage_id = table_node->getStorageID();
|
|
|
|
column_identifier_parts = { table_storage_id.getDatabaseName(), table_storage_id.getTableName() };
|
|
|
|
}
|
2023-01-26 10:52:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-05 12:19:21 +00:00
|
|
|
column_identifier_parts.push_back(column.name);
|
2023-01-26 10:52:40 +00:00
|
|
|
|
2023-02-05 12:19:21 +00:00
|
|
|
return std::make_shared<ASTIdentifier>(std::move(column_identifier_parts));
|
2022-07-14 11:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|