Avoid the literal type from nullable

Signed-off-by: Jiebin Sun <jiebin.sun@intel.com>
This commit is contained in:
Jiebin Sun 2023-12-21 23:29:29 +08:00
parent b3f3a0c75c
commit d43448e747
3 changed files with 66 additions and 4 deletions

View File

@ -4,7 +4,6 @@
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTIdentifier.h>
namespace DB
{
@ -34,6 +33,10 @@ void RewriteSumFunctionWithSumAndCountMatcher::visit(const ASTFunction & functio
if (!nested_func || !nested_func_supported.contains(Poco::toLower(nested_func->name))|| nested_func->arguments->children.size() != 2)
return;
String alias = nested_func->tryGetAlias();
if (!alias.empty())
return;
size_t column_id = nested_func->arguments->children.size();
for (size_t i = 0; i < nested_func->arguments->children.size(); i++)
@ -45,9 +48,25 @@ void RewriteSumFunctionWithSumAndCountMatcher::visit(const ASTFunction & functio
size_t literal_id = 1 - column_id;
const auto * literal = nested_func->arguments->children[literal_id]->as<ASTLiteral>();
const auto * column = nested_func->arguments->children[column_id]->as<ASTIdentifier>();
if (!literal)
return;
if (!column || !literal)
Field::Types::Which literal_type = literal->value.getType();
if (literal_type != Field::Types::UInt64 &&
literal_type != Field::Types::Int64 &&
literal_type != Field::Types::UInt128 &&
literal_type != Field::Types::Int128 &&
literal_type != Field::Types::UInt256 &&
literal_type != Field::Types::Int256 &&
literal_type != Field::Types::Float64 &&
literal_type != Field::Types::Decimal32 &&
literal_type != Field::Types::Decimal64 &&
literal_type != Field::Types::Decimal128 &&
literal_type != Field::Types::Decimal256)
return;
const auto * column = nested_func->arguments->children[column_id]->as<ASTIdentifier>();
if (!column)
return;
auto pos = IdentifierSemantic::getMembership(*column);
@ -74,7 +93,7 @@ void RewriteSumFunctionWithSumAndCountMatcher::visit(const ASTFunction & functio
std::make_shared<ASTIdentifier>(column_name)
),
makeASTFunction("multiply",
std::make_shared<ASTLiteral>(literal->value),
std::make_shared<ASTLiteral>(* literal),
makeASTFunction("count", std::make_shared<ASTIdentifier>(column_name))
)
);

View File

@ -1,3 +1,27 @@
20
SELECT sum(uint64 + 1 AS i)
FROM test_table
--
20
SELECT sum(uint64) + (1 * count(uint64))
FROM test_table
--
\N
0
WITH CAST(\'1\', \'Nullable(UInt64)\') AS my_literal
SELECT sum(number + my_literal)
FROM numbers(0)
WITH CAST(\'1\', \'Nullable(UInt64)\') AS my_literal
SELECT sum(number) + (my_literal * count())
FROM numbers(0)
--
25.549999999999997
25.549999999999997
SELECT sum(uint64) + (2.11 * count(uint64))
FROM test_table
SELECT sum(uint64) + (2.11 * count(uint64))
FROM test_table
--
25
25
SELECT sum(uint64) + (2 * count(uint64))

View File

@ -13,6 +13,25 @@ INSERT INTO test_table VALUES (3, 3.3, 3.33);
INSERT INTO test_table VALUES (4, 4.4, 4.44);
INSERT INTO test_table VALUES (5, 5.5, 5.55);
SELECT sum(uint64 + 1 AS i) from test_table;
EXPLAIN SYNTAX (SELECT sum(uint64 + 1 AS i) from test_table);
SELECT '--';
SELECT sum(uint64 + 1) AS i from test_table;
EXPLAIN SYNTAX (SELECT sum(uint64 + 1) AS i from test_table);
SELECT '--';
WITH 1::Nullable(UInt64) as my_literal Select sum(number + my_literal) from numbers(0);
WITH 1::Nullable(UInt64) as my_literal Select sum(number) + my_literal * count() from numbers(0);
EXPLAIN SYNTAX (WITH 1::Nullable(UInt64) as my_literal Select sum(number + my_literal) from numbers(0));
EXPLAIN SYNTAX (WITH 1::Nullable(UInt64) as my_literal Select sum(number) + my_literal * count() from numbers(0));
SELECT '--';
SELECT sum(uint64 + 2.11) From test_table;
SELECT sum(uint64) + 2.11 * count(uint64) From test_table;
EXPLAIN SYNTAX (SELECT sum(uint64 + 2.11) From test_table);
EXPLAIN SYNTAX (SELECT sum(uint64) + 2.11 * count(uint64) From test_table);
SELECT '--';
SELECT sum(uint64 + 2) From test_table;
SELECT sum(uint64) + 2 * count(uint64) From test_table;