Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty array handling in arraySplit #7747

Merged
merged 3 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions dbms/src/Columns/ColumnConst.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ class ColumnConst final : public COWHelper<IColumn, ColumnConst>

Field getField() const { return getDataColumn()[0]; }

/// The constant value. It is valid even if the size of the column is 0.
template <typename T>
T getValue() const { return getField().safeGet<NearestFieldType<T>>(); }
};
Expand Down
30 changes: 21 additions & 9 deletions dbms/src/Functions/array/arraySplit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,24 @@ struct ArraySplitImpl

size_t pos = 0;

out_offsets_2.reserve(in_offsets.size()); // the actual size would be equal or larger
out_offsets_2.reserve(in_offsets.size()); // assume the actual size to be equal or larger
out_offsets_1.reserve(in_offsets.size());

for (size_t i = 0; i < in_offsets.size(); ++i)
{
pos += !reverse;
for (; pos < in_offsets[i] - reverse; ++pos)
if (pos < in_offsets[i])
{
if (cut[pos])
out_offsets_2.push_back(pos + reverse);
pos += !reverse;
for (; pos < in_offsets[i] - reverse; ++pos)
{
if (cut[pos])
out_offsets_2.push_back(pos + reverse);
}
pos += reverse;

out_offsets_2.push_back(pos);
}
pos += reverse;

out_offsets_2.push_back(pos);
out_offsets_1.push_back(out_offsets_2.size());
}
}
Expand All @@ -73,13 +77,21 @@ struct ArraySplitImpl
}
else
{
size_t pos = 0;

out_offsets_2.reserve(in_offsets.size());
out_offsets_1.reserve(in_offsets.size());

for (size_t i = 0; i < in_offsets.size(); ++i)
{
out_offsets_2.push_back(in_offsets[i]);
out_offsets_1.push_back(i + 1);
if (pos < in_offsets[i])
{
pos = in_offsets[i];

out_offsets_2.push_back(pos);
}

out_offsets_1.push_back(out_offsets_2.size());
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions dbms/tests/queries/0_stateless/01015_array_split.reference
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
[[1],[2],[3],[4],[5]]
[[1,2],[3,4],[5]]
[[1],[2,3],[4,5]]
[[]]
[[]]
[]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be it's better to return [[]] for empty array to make result more consistent with arraySplit(x -> 0, [1, 2, 3, 4, 5]), which returns [[1,2,3,4,5]] (array, which contains the source array)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok for now.

[]
[]
[]
[]
[]
[[1]]
Expand Down
2 changes: 2 additions & 0 deletions dbms/tests/queries/0_stateless/01015_array_split.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ SELECT arraySplit(x -> 0, []);
SELECT arrayReverseSplit(x -> 0, []);
SELECT arraySplit(x -> 1, []);
SELECT arrayReverseSplit(x -> 1, []);
SELECT arraySplit(x -> x, emptyArrayUInt8());
SELECT arrayReverseSplit(x -> x, emptyArrayUInt8());

SELECT arraySplit(x -> x % 2 = 1, [1]);
SELECT arrayReverseSplit(x -> x % 2 = 1, [1]);
Expand Down