Support toUInt8/toInt8 for if constant condition optimization.

This commit is contained in:
Nikolai Kochetov 2021-11-26 15:38:32 +03:00
parent b68f0df8b4
commit 185c20cf88
3 changed files with 32 additions and 0 deletions

View File

@ -49,6 +49,16 @@ static bool tryExtractConstValueFromCondition(const ASTPtr & condition, bool & v
}
}
}
else if (function->name == "toUInt8" || function->name == "toInt8")
{
if (const auto * expr_list = function->arguments->as<ASTExpressionList>())
{
if (expr_list->children.size() != 1)
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Function {} must have exactly two arguments", function->name);
return tryExtractConstValueFromCondition(expr_list->children.at(0), value);
}
}
}
return false;

View File

@ -0,0 +1,8 @@
42
42
42
42
42
42
42
42

View File

@ -0,0 +1,14 @@
drop table if exists test;
-- this queries does not have to pass, but they works historically
-- let's support this while can, see #31687
create table test (x String) Engine=StripeLog;
insert into test values (0);
select if(0, y, 42) from test;
select if(1, 42, y) from test;
select if(toUInt8(0), y, 42) from test;
select if(toInt8(0), y, 42) from test;
select if(toUInt8(1), 42, y) from test;
select if(toInt8(1), 42, y) from test;
select if(toUInt8(toUInt8(0)), y, 42) from test;
select if(cast(cast(0, 'UInt8'), 'UInt8'), y, 42) from test;
drop table if exists t;