2021-06-14 04:13:35 +00:00
|
|
|
#include <Common/FieldVisitorToString.h>
|
|
|
|
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/WriteBufferFromString.h>
|
|
|
|
#include <IO/Operators.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CANNOT_PRINT_FLOAT_OR_DOUBLE_NUMBER;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static inline String formatQuoted(T x)
|
|
|
|
{
|
|
|
|
WriteBufferFromOwnString wb;
|
|
|
|
|
2023-12-21 10:42:04 +00:00
|
|
|
if constexpr (is_decimal_field<T>)
|
|
|
|
{
|
|
|
|
writeChar('\'', wb);
|
|
|
|
writeText(x.getValue(), x.getScale(), wb, {});
|
|
|
|
writeChar('\'', wb);
|
|
|
|
}
|
|
|
|
else if constexpr (is_big_int_v<T>)
|
|
|
|
{
|
|
|
|
writeChar('\'', wb);
|
|
|
|
writeText(x, wb);
|
|
|
|
writeChar('\'', wb);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// While `writeQuoted` sounds like it will always write the value in quotes,
|
|
|
|
/// in fact it means: write according to the rules of the quoted format, like VALUES,
|
|
|
|
/// where strings, dates, date-times, UUID are in quotes, and numbers are not.
|
|
|
|
|
|
|
|
/// That's why we take extra care to put Decimal and big integers inside quotes
|
|
|
|
/// when formatting literals in SQL language,
|
|
|
|
/// because it is different from the quoted formats like VALUES.
|
|
|
|
|
|
|
|
/// In fact, there are no Decimal and big integer literals in SQL,
|
|
|
|
/// but they can appear if we format the query from a modified AST.
|
|
|
|
|
|
|
|
/// We can fix this idiosyncrasy later.
|
|
|
|
|
|
|
|
writeQuoted(x, wb);
|
|
|
|
}
|
|
|
|
return wb.str();
|
2021-06-14 04:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** In contrast to writeFloatText (and writeQuoted),
|
|
|
|
* even if number looks like integer after formatting, prints decimal point nevertheless (for example, Float64(1) is printed as 1.).
|
|
|
|
* - because resulting text must be able to be parsed back as Float64 by query parser (otherwise it will be parsed as integer).
|
|
|
|
*
|
|
|
|
* Trailing zeros after decimal point are omitted.
|
|
|
|
*
|
|
|
|
* NOTE: Roundtrip may lead to loss of precision.
|
|
|
|
*/
|
|
|
|
static String formatFloat(const Float64 x)
|
|
|
|
{
|
|
|
|
DoubleConverter<true>::BufferType buffer;
|
|
|
|
double_conversion::StringBuilder builder{buffer, sizeof(buffer)};
|
|
|
|
|
|
|
|
const auto result = DoubleConverter<true>::instance().ToShortest(x, &builder);
|
|
|
|
|
|
|
|
if (!result)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::CANNOT_PRINT_FLOAT_OR_DOUBLE_NUMBER, "Cannot print float or double number");
|
2021-06-14 04:13:35 +00:00
|
|
|
|
|
|
|
return { buffer, buffer + builder.position() };
|
|
|
|
}
|
|
|
|
|
2021-08-29 19:00:00 +00:00
|
|
|
String FieldVisitorToString::operator() (const Null & x) const { return x.isNegativeInfinity() ? "-Inf" : (x.isPositiveInfinity() ? "+Inf" : "NULL"); }
|
2021-06-14 04:13:35 +00:00
|
|
|
String FieldVisitorToString::operator() (const UInt64 & x) const { return formatQuoted(x); }
|
|
|
|
String FieldVisitorToString::operator() (const Int64 & x) const { return formatQuoted(x); }
|
|
|
|
String FieldVisitorToString::operator() (const Float64 & x) const { return formatFloat(x); }
|
|
|
|
String FieldVisitorToString::operator() (const String & x) const { return formatQuoted(x); }
|
|
|
|
String FieldVisitorToString::operator() (const DecimalField<Decimal32> & x) const { return formatQuoted(x); }
|
|
|
|
String FieldVisitorToString::operator() (const DecimalField<Decimal64> & x) const { return formatQuoted(x); }
|
|
|
|
String FieldVisitorToString::operator() (const DecimalField<Decimal128> & x) const { return formatQuoted(x); }
|
|
|
|
String FieldVisitorToString::operator() (const DecimalField<Decimal256> & x) const { return formatQuoted(x); }
|
|
|
|
String FieldVisitorToString::operator() (const Int128 & x) const { return formatQuoted(x); }
|
|
|
|
String FieldVisitorToString::operator() (const UInt128 & x) const { return formatQuoted(x); }
|
|
|
|
String FieldVisitorToString::operator() (const UInt256 & x) const { return formatQuoted(x); }
|
|
|
|
String FieldVisitorToString::operator() (const Int256 & x) const { return formatQuoted(x); }
|
|
|
|
String FieldVisitorToString::operator() (const UUID & x) const { return formatQuoted(x); }
|
2022-11-14 14:17:17 +00:00
|
|
|
String FieldVisitorToString::operator() (const IPv4 & x) const { return formatQuoted(x); }
|
|
|
|
String FieldVisitorToString::operator() (const IPv6 & x) const { return formatQuoted(x); }
|
2021-06-14 04:13:35 +00:00
|
|
|
String FieldVisitorToString::operator() (const AggregateFunctionStateData & x) const { return formatQuoted(x.data); }
|
2022-01-18 11:52:27 +00:00
|
|
|
String FieldVisitorToString::operator() (const bool & x) const { return x ? "true" : "false"; }
|
2023-02-03 16:07:02 +00:00
|
|
|
String FieldVisitorToString::operator() (const CustomType & x) const { return x.toString(); }
|
2021-06-14 04:13:35 +00:00
|
|
|
|
|
|
|
String FieldVisitorToString::operator() (const Array & x) const
|
|
|
|
{
|
|
|
|
WriteBufferFromOwnString wb;
|
|
|
|
|
|
|
|
wb << '[';
|
|
|
|
for (Array::const_iterator it = x.begin(); it != x.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it != x.begin())
|
|
|
|
wb.write(", ", 2);
|
|
|
|
wb << applyVisitor(*this, *it);
|
|
|
|
}
|
|
|
|
wb << ']';
|
|
|
|
|
|
|
|
return wb.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
String FieldVisitorToString::operator() (const Tuple & x) const
|
|
|
|
{
|
|
|
|
WriteBufferFromOwnString wb;
|
|
|
|
|
|
|
|
// For single-element tuples we must use the explicit tuple() function,
|
|
|
|
// or they will be parsed back as plain literals.
|
|
|
|
if (x.size() > 1)
|
|
|
|
{
|
|
|
|
wb << '(';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wb << "tuple(";
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto it = x.begin(); it != x.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it != x.begin())
|
|
|
|
wb << ", ";
|
|
|
|
wb << applyVisitor(*this, *it);
|
|
|
|
}
|
|
|
|
wb << ')';
|
|
|
|
|
|
|
|
return wb.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
String FieldVisitorToString::operator() (const Map & x) const
|
|
|
|
{
|
|
|
|
WriteBufferFromOwnString wb;
|
|
|
|
|
2023-02-20 18:30:55 +00:00
|
|
|
wb << '[';
|
2021-06-14 04:13:35 +00:00
|
|
|
for (auto it = x.begin(); it != x.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it != x.begin())
|
|
|
|
wb << ", ";
|
|
|
|
wb << applyVisitor(*this, *it);
|
|
|
|
}
|
2023-02-20 18:30:55 +00:00
|
|
|
wb << ']';
|
2021-06-14 04:13:35 +00:00
|
|
|
|
|
|
|
return wb.str();
|
|
|
|
}
|
|
|
|
|
2021-08-20 21:11:22 +00:00
|
|
|
String FieldVisitorToString::operator() (const Object & x) const
|
|
|
|
{
|
|
|
|
WriteBufferFromOwnString wb;
|
|
|
|
|
|
|
|
wb << '{';
|
|
|
|
for (auto it = x.begin(); it != x.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it != x.begin())
|
|
|
|
wb << ", ";
|
|
|
|
|
|
|
|
writeDoubleQuoted(it->first, wb);
|
|
|
|
wb << ": " << applyVisitor(*this, it->second);
|
|
|
|
}
|
|
|
|
wb << '}';
|
|
|
|
|
|
|
|
return wb.str();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-05 16:55:00 +00:00
|
|
|
String convertFieldToString(const Field & field)
|
|
|
|
{
|
|
|
|
if (field.getType() == Field::Types::Which::String)
|
|
|
|
return field.get<String>();
|
|
|
|
return applyVisitor(FieldVisitorToString(), field);
|
2021-06-14 04:13:35 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 16:55:00 +00:00
|
|
|
}
|