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>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2022-07-20 10:34:29 +00:00
|
|
|
ColumnNode::ColumnNode(NameAndTypePair column_, QueryTreeNodeWeakPtr column_source_)
|
|
|
|
: column(std::move(column_))
|
|
|
|
, column_source(std::move(column_source_))
|
|
|
|
{
|
2022-08-15 16:34:10 +00:00
|
|
|
children.resize(children_size);
|
2022-07-20 10:34:29 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 16:34:10 +00:00
|
|
|
ColumnNode::ColumnNode(NameAndTypePair column_, QueryTreeNodePtr expression_node_, QueryTreeNodeWeakPtr column_source_)
|
2022-07-20 10:34:29 +00:00
|
|
|
: column(std::move(column_))
|
|
|
|
, column_source(std::move(column_source_))
|
|
|
|
{
|
2022-08-15 16:34:10 +00:00
|
|
|
children.resize(children_size);
|
|
|
|
children[expression_child_index] = std::move(expression_node_);
|
|
|
|
}
|
|
|
|
|
2022-07-14 11:20:16 +00:00
|
|
|
QueryTreeNodePtr ColumnNode::getColumnSource() const
|
|
|
|
{
|
|
|
|
auto lock = column_source.lock();
|
|
|
|
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
|
|
|
|
{
|
|
|
|
auto lock = column_source.lock();
|
|
|
|
if (!lock)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return lock;
|
|
|
|
}
|
|
|
|
|
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-07-14 11:20:16 +00:00
|
|
|
auto column_source_ptr = column_source.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);
|
2022-08-20 14:01:50 +00:00
|
|
|
if (column != rhs_typed.column)
|
2022-07-15 13:32:53 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
auto source_ptr = column_source.lock();
|
|
|
|
auto rhs_source_ptr = rhs_typed.column_source.lock();
|
|
|
|
|
|
|
|
if (!source_ptr && !rhs_source_ptr)
|
|
|
|
return true;
|
|
|
|
else if (source_ptr && !rhs_source_ptr)
|
|
|
|
return false;
|
|
|
|
else if (!source_ptr && rhs_source_ptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return source_ptr->isEqualImpl(*rhs_source_ptr);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
auto column_source_ptr = column_source.lock();
|
|
|
|
if (column_source_ptr)
|
|
|
|
column_source_ptr->updateTreeHashImpl(hash_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
QueryTreeNodePtr ColumnNode::cloneImpl() const
|
|
|
|
{
|
2022-08-20 14:01:50 +00:00
|
|
|
auto clone_result = std::make_shared<ColumnNode>(column, column_source);
|
|
|
|
return clone_result;
|
2022-07-14 11:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColumnNode::getPointersToUpdateAfterClone(QueryTreePointersToUpdate & pointers_to_update)
|
|
|
|
{
|
|
|
|
/** This method is called on node returned from `cloneImpl`. Check IQueryTreeNode.h interface.
|
|
|
|
* old pointer is current column source pointer.
|
|
|
|
* update place is address of column source.
|
|
|
|
*/
|
|
|
|
const auto * old_pointer = getColumnSource().get();
|
|
|
|
pointers_to_update.emplace_back(old_pointer, &column_source);
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTPtr ColumnNode::toASTImpl() const
|
|
|
|
{
|
|
|
|
return std::make_shared<ASTIdentifier>(column.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|