Do not add extra parentheses for tuple negate unary operator

This commit is contained in:
vdimir 2021-09-30 13:47:29 +03:00
parent 192633cb9b
commit 4acd8f3147
No known key found for this signature in database
GPG Key ID: 9B404D301C0CC7EB
3 changed files with 9 additions and 1 deletions

View File

@ -4,6 +4,7 @@
#include <Common/FieldVisitorToString.h>
#include <Common/SipHash.h>
#include <Common/typeid_cast.h>
#include <DataTypes/IDataType.h>
#include <DataTypes/NumberTraits.h>
#include <IO/Operators.h>
#include <IO/WriteBufferFromString.h>
@ -243,8 +244,11 @@ void ASTFunction::formatImplWithoutAlias(const FormatSettings & settings, Format
const auto * literal = arguments->children[0]->as<ASTLiteral>();
const auto * function = arguments->children[0]->as<ASTFunction>();
bool negate = name == "negate";
bool is_tuple = literal && literal->value.getType() == Field::Types::Tuple;
// do not add parentheses for tuple literal, otherwise extra parens will be added `-((3, 7, 3), 1)` -> `-(((3, 7, 3), 1))`
bool literal_need_parens = literal && !is_tuple;
// negate always requires parentheses, otherwise -(-1) will be printed as --1
bool negate_need_parens = negate && (literal || (function && function->name == "negate"));
bool negate_need_parens = negate && (literal_need_parens || (function && function->name == "negate"));
// We don't need parentheses around a single literal.
bool need_parens = !literal && frame.need_parens && !negate_need_parens;

View File

@ -71,3 +71,4 @@
3.6060655943063797
4.3208683194033215
\N
SELECT -((3, 7, 3), 100)

View File

@ -101,3 +101,6 @@ SELECT LpNorm((1, 2, 3), -1.); -- { serverError 69 }
SELECT LpNorm((1, 2, 3), -1); -- { serverError 44 }
SELECT LpNorm((1, 2, 3), 0.); -- { serverError 69 }
SELECT cosineDistance(materialize((NULL, -2147483648)), (1048577, 1048575));
-- not extra parentheses
EXPLAIN SYNTAX SELECT -((3, 7, 3), 100);