2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/SipHash.h>
|
2021-06-14 04:13:35 +00:00
|
|
|
#include <Common/FieldVisitorToString.h>
|
|
|
|
#include <Common/FieldVisitorHash.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2020-05-13 14:14:05 +00:00
|
|
|
#include <IO/WriteBufferFromString.h>
|
|
|
|
#include <IO/Operators.h>
|
2015-06-29 05:46:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-01-23 13:20:03 +00:00
|
|
|
void ASTLiteral::updateTreeHashImpl(SipHash & hash_state) const
|
|
|
|
{
|
|
|
|
const char * prefix = "Literal_";
|
|
|
|
hash_state.update(prefix, strlen(prefix));
|
|
|
|
applyVisitor(FieldVisitorHash(hash_state), value);
|
|
|
|
}
|
|
|
|
|
2022-04-28 11:47:00 +00:00
|
|
|
ASTPtr ASTLiteral::clone() const
|
|
|
|
{
|
|
|
|
auto res = std::make_shared<ASTLiteral>(*this);
|
|
|
|
res->unique_column_name = {};
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:56:20 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2020-05-13 14:14:05 +00:00
|
|
|
/// Writes 'tuple' word before tuple literals for backward compatibility reasons.
|
|
|
|
class FieldVisitorToColumnName : public StaticVisitor<String>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
template<typename T>
|
|
|
|
String operator() (const T & x) const { return visitor(x); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
FieldVisitorToString visitor;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
String FieldVisitorToColumnName::operator() (const Tuple & x) const
|
|
|
|
{
|
|
|
|
WriteBufferFromOwnString wb;
|
|
|
|
|
|
|
|
wb << "tuple(";
|
|
|
|
for (auto it = x.begin(); it != x.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it != x.begin())
|
|
|
|
wb << ", ";
|
|
|
|
wb << applyVisitor(*this, *it);
|
|
|
|
}
|
|
|
|
wb << ')';
|
|
|
|
|
|
|
|
return wb.str();
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:56:20 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 18:03:24 +00:00
|
|
|
void ASTLiteral::appendColumnNameImpl(WriteBuffer & ostr) const
|
2021-06-16 17:56:20 +00:00
|
|
|
{
|
2021-08-03 18:03:24 +00:00
|
|
|
if (use_legacy_column_name_of_tuple)
|
|
|
|
{
|
2021-06-16 17:56:20 +00:00
|
|
|
appendColumnNameImplLegacy(ostr);
|
2021-08-03 18:03:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-06-16 17:56:20 +00:00
|
|
|
|
2020-03-19 01:15:01 +00:00
|
|
|
/// 100 - just arbitrary value.
|
2020-03-24 15:56:46 +00:00
|
|
|
constexpr auto min_elements_for_hashing = 100;
|
2020-03-19 01:15:01 +00:00
|
|
|
|
2021-06-16 17:56:20 +00:00
|
|
|
/// Special case for very large arrays and tuples. Instead of listing all elements, will use hash of them.
|
|
|
|
/// (Otherwise column name will be too long, that will lead to significant slowdown of expression analysis.)
|
|
|
|
auto type = value.getType();
|
|
|
|
if ((type == Field::Types::Array && value.get<const Array &>().size() > min_elements_for_hashing)
|
|
|
|
|| (type == Field::Types::Tuple && value.get<const Tuple &>().size() > min_elements_for_hashing))
|
|
|
|
{
|
|
|
|
SipHash hash;
|
|
|
|
applyVisitor(FieldVisitorHash(hash), value);
|
|
|
|
UInt64 low, high;
|
|
|
|
hash.get128(low, high);
|
|
|
|
|
|
|
|
writeCString(type == Field::Types::Array ? "__array_" : "__tuple_", ostr);
|
|
|
|
writeText(low, ostr);
|
|
|
|
ostr.write('_');
|
|
|
|
writeText(high, ostr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
String column_name = applyVisitor(FieldVisitorToString(), value);
|
|
|
|
writeString(column_name, ostr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTLiteral::appendColumnNameImplLegacy(WriteBuffer & ostr) const
|
|
|
|
{
|
|
|
|
/// 100 - just arbitrary value.
|
|
|
|
constexpr auto min_elements_for_hashing = 100;
|
|
|
|
|
2020-05-13 14:14:05 +00:00
|
|
|
/// Special case for very large arrays. Instead of listing all elements, will use hash of them.
|
2017-01-06 17:41:19 +00:00
|
|
|
/// (Otherwise column name will be too long, that will lead to significant slowdown of expression analysis.)
|
2020-03-19 01:15:01 +00:00
|
|
|
auto type = value.getType();
|
2020-05-13 14:14:05 +00:00
|
|
|
if ((type == Field::Types::Array && value.get<const Array &>().size() > min_elements_for_hashing))
|
2015-06-29 05:46:55 +00:00
|
|
|
{
|
|
|
|
SipHash hash;
|
2017-01-06 17:41:19 +00:00
|
|
|
applyVisitor(FieldVisitorHash(hash), value);
|
2015-06-29 05:46:55 +00:00
|
|
|
UInt64 low, high;
|
|
|
|
hash.get128(low, high);
|
|
|
|
|
2020-05-13 14:14:05 +00:00
|
|
|
writeCString("__array_", ostr);
|
2018-06-27 16:34:11 +00:00
|
|
|
writeText(low, ostr);
|
|
|
|
ostr.write('_');
|
|
|
|
writeText(high, ostr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-13 14:14:05 +00:00
|
|
|
String column_name = applyVisitor(FieldVisitorToColumnName(), value);
|
2018-06-27 16:34:11 +00:00
|
|
|
writeString(column_name, ostr);
|
|
|
|
}
|
2015-06-29 05:46:55 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 16:05:40 +00:00
|
|
|
void ASTLiteral::formatImplWithoutAlias(const FormatSettings & settings, IAST::FormatState &, IAST::FormatStateStacked) const
|
|
|
|
{
|
|
|
|
settings.ostr << applyVisitor(FieldVisitorToString(), value);
|
|
|
|
}
|
|
|
|
|
2015-06-29 05:46:55 +00:00
|
|
|
}
|