From 8d09c7f6182772e3d9172c9549111163a2238aad Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Wed, 3 Jun 2015 06:55:29 +0300 Subject: [PATCH] dbms: additions [#METR-15987]. --- .../include/DB/Functions/FunctionsTransform.h | 42 ++++++---- ...0165_transform_non_const_default.reference | 84 +++++++++++++++++++ .../00165_transform_non_const_default.sql | 12 +++ 3 files changed, 120 insertions(+), 18 deletions(-) create mode 100644 dbms/tests/queries/0_stateless/00165_transform_non_const_default.reference create mode 100644 dbms/tests/queries/0_stateless/00165_transform_non_const_default.sql diff --git a/dbms/include/DB/Functions/FunctionsTransform.h b/dbms/include/DB/Functions/FunctionsTransform.h index b1b3a2ce79a..87419d67bf7 100644 --- a/dbms/include/DB/Functions/FunctionsTransform.h +++ b/dbms/include/DB/Functions/FunctionsTransform.h @@ -729,27 +729,33 @@ private: if (arguments.size() == 4) { - const IColumnConst * default_col = dynamic_cast(&*block.getByPosition(arguments[3]).column); + const IColumn * default_col = block.getByPosition(arguments[3]).column.get(); + const IColumnConst * const_default_col = dynamic_cast(default_col); - if (default_col) + if (const_default_col) + const_default_value = (*const_default_col)[0]; + + /// Нужно ли преобразовать элементы to и default_value к наименьшему общему типу, который является Float64? + bool default_col_is_float = + typeid_cast(default_col) + || typeid_cast(default_col) + || typeid_cast(default_col) + || typeid_cast(default_col); + + bool to_is_float = to[0].getType() == Field::Types::Float64; + + if (default_col_is_float && !to_is_float) { - const_default_value = (*default_col)[0]; - - /// Нужно ли преобразовать элементы to и default_value к наименьшему общему типу, который является Float64? - if (const_default_value.getType() == Field::Types::Float64 && to[0].getType() != Field::Types::Float64) - { - converted_to.resize(to.size()); - for (size_t i = 0, size = to.size(); i < size; ++i) - converted_to[i] = apply_visitor(FieldVisitorConvertToNumber(), to[i]); - used_to = &converted_to; - } - else if (const_default_value.getType() != Field::Types::Float64 && to[0].getType() == Field::Types::Float64) - { - const_default_value = apply_visitor(FieldVisitorConvertToNumber(), const_default_value); - } + converted_to.resize(to.size()); + for (size_t i = 0, size = to.size(); i < size; ++i) + converted_to[i] = apply_visitor(FieldVisitorConvertToNumber(), to[i]); + used_to = &converted_to; + } + else if (!default_col_is_float && to_is_float) + { + if (const_default_col) + const_default_value = apply_visitor(FieldVisitorConvertToNumber(), const_default_value); } - - /// TODO Преобразование для to в случае неконстантного default } /// Замечание: не делается проверка дубликатов в массиве from. diff --git a/dbms/tests/queries/0_stateless/00165_transform_non_const_default.reference b/dbms/tests/queries/0_stateless/00165_transform_non_const_default.reference new file mode 100644 index 00000000000..d66471d9741 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00165_transform_non_const_default.reference @@ -0,0 +1,84 @@ +9999 +9999 +9999 +111 +9999 +222 +9999 +333 +9999 +9999 + + + +hello + +world + +abc + + + + + +hello + +world + +abc + + +- +- +- +hello +- +world +- +abc +- +- +0 +0 +0 +111 +0 +222 +0 +333 +0 +0 +-1 +-1 +-1 +111 +-1 +222 +-1 +333 +-1 +-1 +-1.1 +-1.1 +-1.1 +111 +-1.1 +222 +-1.1 +333 +-1.1 +-1.1 +1 +1 +1 +111 +1 +222.2 +1 +333 +1 +1 +Остальные +Яндекс +Google +Остальные diff --git a/dbms/tests/queries/0_stateless/00165_transform_non_const_default.sql b/dbms/tests/queries/0_stateless/00165_transform_non_const_default.sql new file mode 100644 index 00000000000..f68327f7700 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00165_transform_non_const_default.sql @@ -0,0 +1,12 @@ +SELECT transform(number, [3, 5, 7], [111, 222, 333], materialize(9999)) FROM system.numbers LIMIT 10; +SELECT transform(number, [3, 5, 7], ['hello', 'world', 'abc'], materialize('')) FROM system.numbers LIMIT 10; +SELECT transform(toString(number), ['3', '5', '7'], ['hello', 'world', 'abc'], materialize('')) FROM system.numbers LIMIT 10; +SELECT transform(toString(number), ['3', '5', '7'], ['hello', 'world', 'abc'], materialize('-')) FROM system.numbers LIMIT 10; +SELECT transform(toString(number), ['3', '5', '7'], [111, 222, 333], materialize(0)) FROM system.numbers LIMIT 10; +SELECT transform(toString(number), ['3', '5', '7'], [111, 222, 333], materialize(-1)) FROM system.numbers LIMIT 10; +SELECT transform(toString(number), ['3', '5', '7'], [111, 222, 333], materialize(-1.1)) FROM system.numbers LIMIT 10; +SELECT transform(toString(number), ['3', '5', '7'], [111, 222.2, 333], materialize(1)) FROM system.numbers LIMIT 10; +SELECT transform(1, [2, 3], ['Яндекс', 'Google'], materialize('Остальные')) AS title; +SELECT transform(2, [2, 3], ['Яндекс', 'Google'], materialize('Остальные')) AS title; +SELECT transform(3, [2, 3], ['Яндекс', 'Google'], materialize('Остальные')) AS title; +SELECT transform(4, [2, 3], ['Яндекс', 'Google'], materialize('Остальные')) AS title;