From 3656aa0606bc77f1a5a7bbaddbce99f97dcbe78b Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Tue, 13 Feb 2018 00:06:13 +0300 Subject: [PATCH] Improved performance of function arrayElement with constant array when one of element is an empty array #1889 --- dbms/src/Functions/FunctionsArray.cpp | 10 ++++++++-- .../0_stateless/00570_empty_array_is_const.reference | 3 +++ .../queries/0_stateless/00570_empty_array_is_const.sql | 3 +++ 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 dbms/tests/queries/0_stateless/00570_empty_array_is_const.reference create mode 100644 dbms/tests/queries/0_stateless/00570_empty_array_is_const.sql diff --git a/dbms/src/Functions/FunctionsArray.cpp b/dbms/src/Functions/FunctionsArray.cpp index 1cdf17a500c..3db0e32e794 100644 --- a/dbms/src/Functions/FunctionsArray.cpp +++ b/dbms/src/Functions/FunctionsArray.cpp @@ -61,6 +61,13 @@ void FunctionArray::executeImpl(Block & block, const ColumnNumbers & arguments, { size_t num_elements = arguments.size(); + if (num_elements == 0) + { + /// We should return constant empty array. + block.getByPosition(result).column = block.getByPosition(result).type->createColumnConstWithDefaultValue(block.rows()); + return; + } + const DataTypePtr & return_type = block.getByPosition(result).type; const DataTypePtr & elem_type = static_cast(*return_type).getNestedType(); @@ -91,8 +98,7 @@ void FunctionArray::executeImpl(Block & block, const ColumnNumbers & arguments, columns[i] = columns_holder[i].get(); } - /** Create and fill the result array. - */ + /// Create and fill the result array. auto out = ColumnArray::create(elem_type->createColumn()); IColumn & out_data = out->getData(); diff --git a/dbms/tests/queries/0_stateless/00570_empty_array_is_const.reference b/dbms/tests/queries/0_stateless/00570_empty_array_is_const.reference new file mode 100644 index 00000000000..1eedb9f964c --- /dev/null +++ b/dbms/tests/queries/0_stateless/00570_empty_array_is_const.reference @@ -0,0 +1,3 @@ +Array(Nothing), Const(size = 1, Array(size = 1, UInt64(size = 1), Nothing(size = 0))) +Array(Array(Array(Nothing))), Const(size = 1, Array(size = 1, UInt64(size = 1), Array(size = 1, UInt64(size = 1), Array(size = 1, UInt64(size = 1), Nothing(size = 0))))) +Array(Array(UInt8)), Const(size = 1, Array(size = 1, UInt64(size = 1), Array(size = 2, UInt64(size = 2), UInt8(size = 1)))) diff --git a/dbms/tests/queries/0_stateless/00570_empty_array_is_const.sql b/dbms/tests/queries/0_stateless/00570_empty_array_is_const.sql new file mode 100644 index 00000000000..62d56a7745d --- /dev/null +++ b/dbms/tests/queries/0_stateless/00570_empty_array_is_const.sql @@ -0,0 +1,3 @@ +SELECT dumpColumnStructure([]); +SELECT dumpColumnStructure([[[]]]); +SELECT DISTINCT dumpColumnStructure([[], [1]]) FROM numbers(2);