From eee107fcedd165548d33c37822f80c922f30f32c Mon Sep 17 00:00:00 2001 From: vdimir Date: Tue, 20 Aug 2024 13:38:22 +0000 Subject: [PATCH] fix + tests --- src/Parsers/ASTFunction.cpp | 7 ++- ...3168_inconsistent_ast_formatting.reference | 38 ++++++++++++++ .../03168_inconsistent_ast_formatting.sql | 51 +++++++++++++++++++ 3 files changed, 94 insertions(+), 2 deletions(-) diff --git a/src/Parsers/ASTFunction.cpp b/src/Parsers/ASTFunction.cpp index a7ba60ad3a8..5d84127d9b0 100644 --- a/src/Parsers/ASTFunction.cpp +++ b/src/Parsers/ASTFunction.cpp @@ -332,8 +332,11 @@ void ASTFunction::formatImplWithoutAlias(const FormatSettings & settings, Format const auto * literal = arguments->children[0]->as(); const auto * function = arguments->children[0]->as(); const auto * subquery = arguments->children[0]->as(); - bool is_tuple = literal && literal->value.getType() == Field::Types::Tuple; - bool is_array = literal && literal->value.getType() == Field::Types::Array; + bool is_tuple = (literal && literal->value.getType() == Field::Types::Tuple) + || (function && function->name == "tuple" && function->arguments && function->arguments->children.size() > 1); + bool is_array = (literal && literal->value.getType() == Field::Types::Array) + || (function && function->name == "array"); + /// Do not add parentheses for tuple and array literal, otherwise extra parens will be added `-((3, 7, 3), 1)` -> `-(((3, 7, 3), 1))`, `-[1]` -> `-([1])` bool literal_need_parens = literal && !is_tuple && !is_array; diff --git a/tests/queries/0_stateless/03168_inconsistent_ast_formatting.reference b/tests/queries/0_stateless/03168_inconsistent_ast_formatting.reference index e69de29bb2d..36eb3e954ec 100644 --- a/tests/queries/0_stateless/03168_inconsistent_ast_formatting.reference +++ b/tests/queries/0_stateless/03168_inconsistent_ast_formatting.reference @@ -0,0 +1,38 @@ +-- +0 +SELECT NOT 1 +SELECT NOT 1 +-- +SELECT not(1, 1, 1) +SELECT not(1, 1, 1) +-- +SELECT NOT tuple(1) +SELECT NOT tuple(1) +SELECT NOT tuple(1) +-- +SELECT NOT ((1, 1, 1)) +SELECT NOT ((1, 1, 1)) +SELECT NOT ((1, 1, 1)) +SELECT NOT ((1, 1, 1)) +-- +SELECT NOT [1] +SELECT NOT [1] +SELECT NOT [1] +SELECT NOT [1] +SELECT NOT [1] +-- +SELECT -[1] +SELECT -[1] +SELECT -[1] +SELECT -[1] +SELECT -[1] +-- +(-1,-1,-1) +SELECT -(1, 1, 1) +SELECT -(1, 1, 1) +SELECT -(1, 1, 1) +SELECT -(1, 1, 1) +-- +((-1,-1,-1)) +SELECT -tuple((1, 1, 1)) +SELECT -tuple((1, 1, 1)) diff --git a/tests/queries/0_stateless/03168_inconsistent_ast_formatting.sql b/tests/queries/0_stateless/03168_inconsistent_ast_formatting.sql index 5aac75ab4cf..ace538a90b7 100644 --- a/tests/queries/0_stateless/03168_inconsistent_ast_formatting.sql +++ b/tests/queries/0_stateless/03168_inconsistent_ast_formatting.sql @@ -6,5 +6,56 @@ create table a (`value2` Enum8('Hello' = 1, equals(`Null`, 'World', 2), '!' = 3) create table a (x Int8) engine Memory; create table b empty as a; +SELECT '--'; +SELECT NOT (1); +SELECT formatQuery('SELECT NOT 1'); +SELECT formatQuery('SELECT NOT (1)'); + +SELECT '--'; +SELECT NOT (1, 1, 1); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH } +SELECT formatQuery('SELECT NOT (1, 1, 1)'); +SELECT formatQuery('SELECT not(1, 1, 1)'); + +SELECT '--'; +SELECT NOT ((1,)); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT } +SELECT NOT tuple(1); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT } +SELECT formatQuery('SELECT NOT ((1,))'); +SELECT formatQuery('SELECT NOT (tuple(1))'); +SELECT formatQuery('SELECT NOT tuple(1)'); + +SELECT '--'; SELECT NOT ((1, 1, 1)); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT } +SELECT formatQuery('SELECT NOT ((1, 1, 1))'); +SELECT formatQuery('SELECT not((1, 1, 1))'); +SELECT formatQuery('SELECT not tuple(1, 1, 1)'); +SELECT formatQuery('SELECT not (tuple(1, 1, 1))'); + +SELECT '--'; +SELECT NOT [1]; -- { serverError ILLEGAL_TYPE_OF_ARGUMENT } +SELECT NOT [(1)]; -- { serverError ILLEGAL_TYPE_OF_ARGUMENT } +SELECT formatQuery('SELECT NOT [1]'); +SELECT formatQuery('SELECT NOT array(1)'); +SELECT formatQuery('SELECT NOT (array(1))'); +SELECT formatQuery('SELECT NOT [(1)]'); +SELECT formatQuery('SELECT NOT ([1])'); + +SELECT '--'; +SELECT -[1]; -- { serverError ILLEGAL_TYPE_OF_ARGUMENT } SELECT -[(1)]; -- { serverError ILLEGAL_TYPE_OF_ARGUMENT } +SELECT formatQuery('SELECT -[1]'); +SELECT formatQuery('SELECT -array(1)'); +SELECT formatQuery('SELECT -(array(1))'); +SELECT formatQuery('SELECT -[(1)]'); +SELECT formatQuery('SELECT -([1])'); + +SELECT '--'; +SELECT -(1, 1, 1); +SELECT formatQuery('SELECT -(1, 1, 1)'); +SELECT formatQuery('SELECT negate ((1, 1, 1))'); +SELECT formatQuery('SELECT -tuple(1, 1, 1)'); +SELECT formatQuery('SELECT -(tuple(1, 1, 1))'); + +SELECT '--'; +SELECT -tuple((1, 1, 1)); +SELECT formatQuery('SELECT -((1, 1, 1))'); +SELECT formatQuery('SELECT -tuple((1, 1, 1))');