diff --git a/src/Parsers/ASTFunction.cpp b/src/Parsers/ASTFunction.cpp index 058cd8daa41..3c78c4060d6 100644 --- a/src/Parsers/ASTFunction.cpp +++ b/src/Parsers/ASTFunction.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -243,8 +244,11 @@ void ASTFunction::formatImplWithoutAlias(const FormatSettings & settings, Format const auto * literal = arguments->children[0]->as(); const auto * function = arguments->children[0]->as(); 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; diff --git a/tests/queries/0_stateless/02011_tuple_vector_functions.reference b/tests/queries/0_stateless/02011_tuple_vector_functions.reference index 74e9c1b8a3c..4c5067c7da2 100644 --- a/tests/queries/0_stateless/02011_tuple_vector_functions.reference +++ b/tests/queries/0_stateless/02011_tuple_vector_functions.reference @@ -71,3 +71,4 @@ 3.6060655943063797 4.3208683194033215 \N +SELECT -((3, 7, 3), 100) diff --git a/tests/queries/0_stateless/02011_tuple_vector_functions.sql b/tests/queries/0_stateless/02011_tuple_vector_functions.sql index 419ca753500..f34fb91586c 100644 --- a/tests/queries/0_stateless/02011_tuple_vector_functions.sql +++ b/tests/queries/0_stateless/02011_tuple_vector_functions.sql @@ -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);