ClickHouse/src/Analyzer/ConstantNode.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

71 lines
2.0 KiB
C++
Raw Normal View History

2022-07-14 11:20:16 +00:00
#include <Analyzer/ConstantNode.h>
#include <Common/FieldVisitorToString.h>
#include <Common/SipHash.h>
#include <IO/WriteBuffer.h>
#include <IO/WriteHelpers.h>
#include <IO/Operators.h>
#include <DataTypes/FieldToDataType.h>
#include <Parsers/ASTLiteral.h>
2022-09-06 15:25:52 +00:00
#include <Interpreters/convertFieldToType.h>
2022-07-14 11:20:16 +00:00
namespace DB
{
2022-08-31 15:21:17 +00:00
ConstantNode::ConstantNode(ConstantValuePtr constant_value_)
: constant_value(std::move(constant_value_))
, value_string_dump(applyVisitor(FieldVisitorToString(), constant_value->getValue()))
{
}
2022-07-14 11:20:16 +00:00
ConstantNode::ConstantNode(Field value_, DataTypePtr value_data_type_)
2022-09-06 15:25:52 +00:00
: ConstantNode(std::make_shared<ConstantValue>(convertFieldToTypeOrThrow(value_, *value_data_type_), value_data_type_))
2022-07-14 11:20:16 +00:00
{}
ConstantNode::ConstantNode(Field value_)
2022-09-06 15:25:52 +00:00
: ConstantNode(value_, applyVisitor(FieldToDataType(), value_))
2022-07-14 11:20:16 +00:00
{}
void ConstantNode::dumpTreeImpl(WriteBuffer & buffer, FormatState & format_state, size_t indent) const
2022-07-14 11:20:16 +00:00
{
buffer << std::string(indent, ' ') << "CONSTANT id: " << format_state.getNodeId(this);
if (hasAlias())
buffer << ", alias: " << getAlias();
2022-08-31 15:21:17 +00:00
buffer << ", constant_value: " << constant_value->getValue().dump();
buffer << ", constant_value_type: " << constant_value->getType()->getName();
2022-07-14 11:20:16 +00:00
}
2022-07-15 13:32:53 +00:00
bool ConstantNode::isEqualImpl(const IQueryTreeNode & rhs) const
{
const auto & rhs_typed = assert_cast<const ConstantNode &>(rhs);
2022-08-31 15:21:17 +00:00
return *constant_value == *rhs_typed.constant_value && value_string_dump == rhs_typed.value_string_dump;
2022-07-15 13:32:53 +00:00
}
2022-07-14 11:20:16 +00:00
void ConstantNode::updateTreeHashImpl(HashState & hash_state) const
{
2022-08-31 15:21:17 +00:00
auto type_name = constant_value->getType()->getName();
2022-07-14 11:20:16 +00:00
hash_state.update(type_name.size());
hash_state.update(type_name);
hash_state.update(value_string_dump.size());
hash_state.update(value_string_dump);
}
ASTPtr ConstantNode::toASTImpl() const
{
2022-08-31 15:21:17 +00:00
return std::make_shared<ASTLiteral>(constant_value->getValue());
2022-07-14 11:20:16 +00:00
}
QueryTreeNodePtr ConstantNode::cloneImpl() const
{
2022-08-31 15:21:17 +00:00
return std::make_shared<ConstantNode>(constant_value);
2022-07-14 11:20:16 +00:00
}
}