ignore UB as in negate()

This commit is contained in:
Alexander Kuzmenkov 2021-03-26 20:29:41 +03:00
parent 33647ef3d5
commit cc93055e9e
3 changed files with 14 additions and 1 deletions

View File

@ -224,12 +224,19 @@ void ASTFunction::formatImplWithoutAlias(const FormatSettings & settings, Format
* a literal that is a negative number "-(-1)" or "- -1", this * a literal that is a negative number "-(-1)" or "- -1", this
* can not be formatted as `--1`, since this will be * can not be formatted as `--1`, since this will be
* interpreted as a comment. Instead, negate the literal * 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") if (literal && name == "negate")
{ {
written = applyVisitor( written = applyVisitor(
[&settings](const auto & value) [&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<decltype(value)>; using ValueType = std::decay_t<decltype(value)>;
if constexpr (isDecimalField<ValueType>()) if constexpr (isDecimalField<ValueType>())

View File

@ -13,3 +13,8 @@ SELECT
1, 1,
1, 1,
-1. IN (-1.) -1. IN (-1.)
explain syntax select negate(-9223372036854775808), -(-9223372036854775808), - -9223372036854775808;
SELECT
-9223372036854775808,
-9223372036854775808,
-9223372036854775808

View File

@ -1,3 +1,4 @@
-- { echo } -- { 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(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;