fix + tests

This commit is contained in:
vdimir 2024-08-20 13:38:22 +00:00
parent 765e83db64
commit eee107fced
No known key found for this signature in database
GPG Key ID: 6EE4CE2BEDC51862
3 changed files with 94 additions and 2 deletions

View File

@ -332,8 +332,11 @@ 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;
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;

View File

@ -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))

View File

@ -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))');