diff --git a/src/Parsers/ASTFunction.cpp b/src/Parsers/ASTFunction.cpp index 9027579495a..6871a817351 100644 --- a/src/Parsers/ASTFunction.cpp +++ b/src/Parsers/ASTFunction.cpp @@ -237,7 +237,9 @@ void ASTFunction::formatImplWithoutAlias(const FormatSettings & settings, Format * but the old comment said it is impossible, without mentioning * the reason. We should also negate the nonnegative literals, * for symmetry. We print the negated value without parentheses, - * because they are not needed around a single literal. + * because they are not needed around a single literal. Also we + * use formatting from FieldVisitorToString, so that the type is + * preserved (e.g. -0. is printed with trailing period). */ if (literal && name == "negate") { @@ -257,14 +259,15 @@ void ASTFunction::formatImplWithoutAlias(const FormatSettings & settings, Format // fuzzer. Decimals are always signed, so no need // to deduce the result type like we do for ints. const auto int_value = value.getValue().value; - settings.ostr << ValueType{ + settings.ostr << FieldVisitorToString{}(ValueType{ -int_value, - value.getScale()}; + value.getScale()}); } else if constexpr (std::is_arithmetic_v) { using ResultType = typename NumberTraits::ResultOfNegate::Type; - settings.ostr << -static_cast(value); + settings.ostr << FieldVisitorToString{}( + -static_cast(value)); return true; } diff --git a/tests/queries/0_stateless/01566_negate_formatting.reference b/tests/queries/0_stateless/01566_negate_formatting.reference index 88eda68bb7e..69d79cf929a 100644 --- a/tests/queries/0_stateless/01566_negate_formatting.reference +++ b/tests/queries/0_stateless/01566_negate_formatting.reference @@ -8,10 +8,10 @@ SELECT -1 IN (-1) explain syntax select negate(1.), negate(-1.), - -1., -(-1.), (-1.) in (-1.); SELECT - -1, - 1, - 1, - 1, + -1., + 1., + 1., + 1., -1. IN (-1.) explain syntax select negate(-9223372036854775808), -(-9223372036854775808), - -9223372036854775808; SELECT @@ -27,8 +27,8 @@ SELECT 0 IN (0) explain syntax select negate(0.), negate(-0.), - -0., -(-0.), (-0.) in (-0.); SELECT - -0, - 0, - 0, - 0, + -0., + 0., + 0., + 0., -0. IN (-0.)