diff --git a/src/Parsers/ASTFunction.cpp b/src/Parsers/ASTFunction.cpp index b935704e9f3..d02e82d5ab3 100644 --- a/src/Parsers/ASTFunction.cpp +++ b/src/Parsers/ASTFunction.cpp @@ -224,12 +224,19 @@ void ASTFunction::formatImplWithoutAlias(const FormatSettings & settings, Format * a literal that is a negative number "-(-1)" or "- -1", this * can not be formatted as `--1`, since this will be * interpreted as a comment. Instead, negate the literal - * in place. + * in place. Another possible solution is to use parentheses, + * but the old comment said it is impossible, without mentioning + * the reason. */ if (literal && name == "negate") { written = applyVisitor( [&settings](const auto & value) + // -INT_MAX is negated to -INT_MAX by the negate() + // function, so we can implement this behavior here as + // well. Technically it is an UB to perform such negation + // w/o a cast to unsigned type. + __attribute__((__no_sanitize__("undefined"))) { using ValueType = std::decay_t; if constexpr (isDecimalField()) diff --git a/tests/queries/0_stateless/01566_negate_formatting.reference b/tests/queries/0_stateless/01566_negate_formatting.reference index 8d50cbbf980..b955d4cbbc5 100644 --- a/tests/queries/0_stateless/01566_negate_formatting.reference +++ b/tests/queries/0_stateless/01566_negate_formatting.reference @@ -13,3 +13,8 @@ SELECT 1, 1, -1. IN (-1.) +explain syntax select negate(-9223372036854775808), -(-9223372036854775808), - -9223372036854775808; +SELECT + -9223372036854775808, + -9223372036854775808, + -9223372036854775808 diff --git a/tests/queries/0_stateless/01566_negate_formatting.sql b/tests/queries/0_stateless/01566_negate_formatting.sql index 48ec38acc14..035ff80e8d8 100644 --- a/tests/queries/0_stateless/01566_negate_formatting.sql +++ b/tests/queries/0_stateless/01566_negate_formatting.sql @@ -1,3 +1,4 @@ -- { echo } 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;