Function multiIf constant result support

This commit is contained in:
Maksim Kita 2021-01-25 00:35:49 +03:00
parent 77af612bc5
commit dac6cceac1
3 changed files with 33 additions and 4 deletions

View File

@ -134,7 +134,9 @@ public:
Instruction instruction; Instruction instruction;
size_t source_idx = i + 1; size_t source_idx = i + 1;
if (source_idx == args.size()) bool last_else_branch = source_idx == args.size();
if (last_else_branch)
{ {
/// The last, "else" branch can be treated as a branch with always true condition "else if (true)". /// The last, "else" branch can be treated as a branch with always true condition "else if (true)".
--source_idx; --source_idx;
@ -150,13 +152,15 @@ public:
if (cond_col.column->onlyNull()) if (cond_col.column->onlyNull())
continue; continue;
if (isColumnConst(*cond_col.column)) if (const auto * column_const = checkAndGetColumn<ColumnConst>(*cond_col.column))
{ {
Field value = typeid_cast<const ColumnConst &>(*cond_col.column).getField(); Field value = column_const->getField();
if (value.isNull()) if (value.isNull())
continue; continue;
if (value.get<UInt64>() == 0) if (value.get<UInt64>() == 0)
continue; continue;
instruction.condition_always_true = true; instruction.condition_always_true = true;
} }
else else
@ -189,9 +193,19 @@ public:
break; break;
} }
size_t rows = input_rows_count;
MutableColumnPtr res = return_type->createColumn(); MutableColumnPtr res = return_type->createColumn();
/// Special case if first instruction condition is always true and source is constant
if (instructions.size() == 1 && instructions.front().source_is_constant
&& instructions.front().condition_always_true)
{
auto & instruction = instructions.front();
res->insertFrom(assert_cast<const ColumnConst &>(*instruction.source).getDataColumn(), 0);
return ColumnConst::create(std::move(res), instruction.source->size());
}
size_t rows = input_rows_count;
for (size_t i = 0; i < rows; ++i) for (size_t i = 0; i < rows; ++i)
{ {
for (const auto & instruction : instructions) for (const auto & instruction : instructions)

View File

@ -18662,3 +18662,7 @@ Miscellaneous
1 Москва 1 Москва
65 Москва 65 Москва
208 Москва 208 Москва
Constant result
Value 1
Value 1
ValueFirst 0

View File

@ -1938,3 +1938,14 @@ INSERT INTO multi_if_check(col1) VALUES (10418),(235),(25),(179),(26030),(28381)
SELECT DISTINCT col1, multiIf(col1 != 213, 'Москва', 'Мир') AS k FROM multi_if_check LIMIT 10; SELECT DISTINCT col1, multiIf(col1 != 213, 'Москва', 'Мир') AS k FROM multi_if_check LIMIT 10;
DROP TABLE IF EXISTS multi_if_check; DROP TABLE IF EXISTS multi_if_check;
SELECT 'Constant result';
CREATE TABLE multi_if_check(value String) ENGINE=TinyLog;
INSERT INTO multi_if_check VALUES ('1');
SELECT multiIf(2 > 1, 'Value', 'ElseValue') as a, isConstant(a) FROM multi_if_check;
SELECT multiIf(2 > 1, 'Value', value) as a, isConstant(a) FROM multi_if_check;
SELECT multiIf(value == '1', 'ValueFirst', 2 > 1, 'ValueSecond', 'ElseValue') as a, isConstant(a) FROM multi_if_check;
DROP TABLE IF EXISTS multi_if_check;