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-11-29 11:35:05 +00:00
|
|
|
ConstantNode::ConstantNode(ConstantValuePtr constant_value_, QueryTreeNodePtr source_expression)
|
2022-10-07 10:44:28 +00:00
|
|
|
: IQueryTreeNode(children_size)
|
|
|
|
, constant_value(std::move(constant_value_))
|
2022-10-06 19:36:06 +00:00
|
|
|
, value_string(applyVisitor(FieldVisitorToString(), constant_value->getValue()))
|
2022-08-31 15:21:17 +00:00
|
|
|
{
|
2022-11-29 11:35:05 +00:00
|
|
|
children[source_child_index] = std::move(source_expression);
|
2022-08-31 15:21:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 11:35:05 +00:00
|
|
|
ConstantNode::ConstantNode(ConstantValuePtr constant_value_)
|
|
|
|
: ConstantNode(constant_value_, nullptr /*source_expression*/)
|
|
|
|
{}
|
|
|
|
|
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
|
|
|
{}
|
|
|
|
|
2022-07-19 10:54:45 +00:00
|
|
|
void ConstantNode::dumpTreeImpl(WriteBuffer & buffer, FormatState & format_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, ' ') << "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-11-29 11:35:05 +00:00
|
|
|
|
|
|
|
if (getSourceExpression())
|
|
|
|
{
|
|
|
|
buffer << '\n' << std::string(indent + 2, ' ') << "EXPRESSION " << '\n';
|
|
|
|
getSourceExpression()->dumpTreeImpl(buffer, format_state, indent + 4);
|
|
|
|
}
|
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-10-06 19:36:06 +00:00
|
|
|
return *constant_value == *rhs_typed.constant_value && value_string == rhs_typed.value_string;
|
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);
|
|
|
|
|
2022-10-06 19:36:06 +00:00
|
|
|
hash_state.update(value_string.size());
|
|
|
|
hash_state.update(value_string);
|
2022-07-14 11:20:16 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 10:25:58 +00:00
|
|
|
QueryTreeNodePtr ConstantNode::cloneImpl() const
|
2022-07-14 11:20:16 +00:00
|
|
|
{
|
2022-10-10 10:25:58 +00:00
|
|
|
return std::make_shared<ConstantNode>(constant_value);
|
2022-07-14 11:20:16 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 10:25:58 +00:00
|
|
|
ASTPtr ConstantNode::toASTImpl() const
|
2022-07-14 11:20:16 +00:00
|
|
|
{
|
2022-10-10 10:25:58 +00:00
|
|
|
return std::make_shared<ASTLiteral>(constant_value->getValue());
|
2022-07-14 11:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|