Merge pull request #66840 from ClickHouse/fix-inconsistent-formatting-of-not-subquery

Fix inconsistent formatting of `NOT ((SELECT ...))`
This commit is contained in:
Alexey Milovidov 2024-07-23 00:17:13 +00:00 committed by GitHub
commit bd9bfc34a9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 4 deletions

View File

@ -329,19 +329,23 @@ void ASTFunction::formatImplWithoutAlias(const FormatSettings & settings, Format
const auto * literal = arguments->children[0]->as<ASTLiteral>();
const auto * function = arguments->children[0]->as<ASTFunction>();
const auto * subquery = arguments->children[0]->as<ASTSubquery>();
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))`
/// 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 inside_parens = name == "negate" && (literal_need_parens || (function && function->name == "negate"));
/// Negate always requires parentheses, otherwise -(-1) will be printed as --1
/// Also extra parentheses are needed for subqueries, because NOT can be parsed as a function:
/// not(SELECT 1) cannot be parsed, while not((SELECT 1)) can.
bool inside_parens = (name == "negate" && (literal_need_parens || (function && function->name == "negate")))
|| (subquery && name == "not");
/// We DO need parentheses around a single literal
/// For example, SELECT (NOT 0) + (NOT 0) cannot be transformed into SELECT NOT 0 + NOT 0, since
/// this is equal to SELECT NOT (0 + NOT 0)
bool outside_parens = frame.need_parens && !inside_parens;
// do not add extra parentheses for functions inside negate, i.e. -(-toUInt64(-(1)))
/// Do not add extra parentheses for functions inside negate, i.e. -(-toUInt64(-(1)))
if (inside_parens)
nested_need_parens.need_parens = false;

View File

@ -0,0 +1 @@
SELECT NOT ((SELECT 1))

View File

@ -0,0 +1,7 @@
#!/usr/bin/env bash
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=../shell_config.sh
. "$CURDIR"/../shell_config.sh
$CLICKHOUSE_FORMAT --oneline --query "SELECT NOT((SELECT 1))"