Merge pull request #23201 from ClickHouse/aku/negate-stable-formatting

more stable formatting for negate()
This commit is contained in:
Alexander Kuzmenkov 2021-04-19 16:48:43 +03:00 committed by GitHub
commit dd55fd8954
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 20 deletions

View File

@ -3,6 +3,7 @@
#include <Common/quoteString.h>
#include <Common/SipHash.h>
#include <Common/typeid_cast.h>
#include <DataTypes/NumberTraits.h>
#include <IO/Operators.h>
#include <IO/WriteBufferFromString.h>
#include <IO/WriteHelpers.h>
@ -234,7 +235,11 @@ void ASTFunction::formatImplWithoutAlias(const FormatSettings & settings, Format
* interpreted as a comment. Instead, negate the literal
* in place. Another possible solution is to use parentheses,
* but the old comment said it is impossible, without mentioning
* the reason.
* 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. Also we
* use formatting from FieldVisitorToString, so that the type is
* preserved (e.g. -0. is printed with trailing period).
*/
if (literal && name == "negate")
{
@ -251,26 +256,18 @@ void ASTFunction::formatImplWithoutAlias(const FormatSettings & settings, Format
{
// The parser doesn't create decimal literals, but
// they can be produced by constant folding or the
// fuzzer.
// 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;
// We compare to zero so we don't care about scale.
if (int_value >= 0)
{
return false;
}
settings.ostr << ValueType{-int_value,
value.getScale()};
settings.ostr << FieldVisitorToString{}(ValueType{
-int_value,
value.getScale()});
}
else if constexpr (std::is_arithmetic_v<ValueType>)
{
if (value >= 0)
{
return false;
}
// We don't need parentheses around a single
// literal.
settings.ostr << -value;
using ResultType = typename NumberTraits::ResultOfNegate<ValueType>::Type;
settings.ostr << FieldVisitorToString{}(
-static_cast<ResultType>(value));
return true;
}

View File

@ -9,12 +9,26 @@ SELECT
explain syntax select negate(1.), negate(-1.), - -1., -(-1.), (-1.) in (-1.);
SELECT
-1.,
1,
1,
1,
1.,
1.,
1.,
-1. IN (-1.)
explain syntax select negate(-9223372036854775808), -(-9223372036854775808), - -9223372036854775808;
SELECT
-9223372036854775808,
-9223372036854775808,
-9223372036854775808
explain syntax select negate(0), negate(-0), - -0, -(-0), (-0) in (-0);
SELECT
0,
0,
0,
0,
0 IN (0)
explain syntax select negate(0.), negate(-0.), - -0., -(-0.), (-0.) in (-0.);
SELECT
-0.,
0.,
0.,
0.,
-0. IN (-0.)

View File

@ -2,3 +2,5 @@
explain syntax select negate(1), negate(-1), - -1, -(-1), (-1) in (-1);
explain syntax select negate(1.), negate(-1.), - -1., -(-1.), (-1.) in (-1.);
explain syntax select negate(-9223372036854775808), -(-9223372036854775808), - -9223372036854775808;
explain syntax select negate(0), negate(-0), - -0, -(-0), (-0) in (-0);
explain syntax select negate(0.), negate(-0.), - -0., -(-0.), (-0.) in (-0.);