diff --git a/src/Columns/ColumnLowCardinality.cpp b/src/Columns/ColumnLowCardinality.cpp index 5c174d57b32b854e8d17005d0120376db6c14a50..df714763225f184776b913549a5ad46df28ad287 100644 --- a/src/Columns/ColumnLowCardinality.cpp +++ b/src/Columns/ColumnLowCardinality.cpp @@ -352,7 +352,7 @@ void ColumnLowCardinality::updatePermutation(bool reverse, size_t limit, int nan auto new_first = first; for (auto j = first + 1; j < last; ++j) { - if (compareAt(new_first, j, *this, nan_direction_hint) != 0) + if (compareAt(res[new_first], res[j], *this, nan_direction_hint) != 0) { if (j - new_first > 1) new_ranges.emplace_back(new_first, j); @@ -376,7 +376,7 @@ void ColumnLowCardinality::updatePermutation(bool reverse, size_t limit, int nan auto new_first = first; for (auto j = first + 1; j < limit; ++j) { - if (getDictionary().compareAt(getIndexes().getUInt(new_first), getIndexes().getUInt(j), getDictionary(), nan_direction_hint) != 0) + if (getDictionary().compareAt(getIndexes().getUInt(res[new_first]), getIndexes().getUInt(res[j]), getDictionary(), nan_direction_hint) != 0) { if (j - new_first > 1) new_ranges.emplace_back(new_first, j); @@ -387,7 +387,7 @@ void ColumnLowCardinality::updatePermutation(bool reverse, size_t limit, int nan auto new_last = limit; for (auto j = limit; j < last; ++j) { - if (getDictionary().compareAt(getIndexes().getUInt(new_first), getIndexes().getUInt(j), getDictionary(), nan_direction_hint) == 0) + if (getDictionary().compareAt(getIndexes().getUInt(res[new_first]), getIndexes().getUInt(res[j]), getDictionary(), nan_direction_hint) == 0) { std::swap(res[new_last], res[j]); ++new_last; diff --git a/tests/queries/0_stateless/01456_low_cardinality_sorting_bugfix.reference b/tests/queries/0_stateless/01456_low_cardinality_sorting_bugfix.reference new file mode 100644 index 0000000000000000000000000000000000000000..cd7dfe76238091b2c1b11aa5906b88b6636655e8 --- /dev/null +++ b/tests/queries/0_stateless/01456_low_cardinality_sorting_bugfix.reference @@ -0,0 +1,31 @@ +green 2020-08-21 18:46:08 +green 2020-08-21 18:46:07 +green 2020-08-21 18:46:06 +red 2020-08-21 18:46:08 +red 2020-08-21 18:46:07 +red 2020-08-21 18:46:06 + + +green 2020-08-21 18:46:08.000 +green 2020-08-21 18:46:07.000 +green 2020-08-21 18:46:06.000 +red 2020-08-21 18:46:08.000 +red 2020-08-21 18:46:07.000 +red 2020-08-21 18:46:06.000 + +------cast to String---- + +green 2020-08-21 18:46:08 +green 2020-08-21 18:46:07 +green 2020-08-21 18:46:06 +red 2020-08-21 18:46:08 +red 2020-08-21 18:46:07 +red 2020-08-21 18:46:06 + + +green 2020-08-21 18:46:08.000 +green 2020-08-21 18:46:07.000 +green 2020-08-21 18:46:06.000 +red 2020-08-21 18:46:08.000 +red 2020-08-21 18:46:07.000 +red 2020-08-21 18:46:06.000 diff --git a/tests/queries/0_stateless/01456_low_cardinality_sorting_bugfix.sql b/tests/queries/0_stateless/01456_low_cardinality_sorting_bugfix.sql new file mode 100644 index 0000000000000000000000000000000000000000..507a798e7b68322d07723033d5a17376ac1ee819 --- /dev/null +++ b/tests/queries/0_stateless/01456_low_cardinality_sorting_bugfix.sql @@ -0,0 +1,41 @@ +drop table if exists order_test1; + +create table order_test1 +( + timestamp DateTime64(3), + color LowCardinality(String) +) engine = MergeTree() ORDER BY tuple(); + +insert into order_test1 values ('2020-08-21 18:46:08.000','red')('2020-08-21 18:46:08.000','green'); +insert into order_test1 values ('2020-08-21 18:46:07.000','red')('2020-08-21 18:46:07.000','green'); +insert into order_test1 values ('2020-08-21 18:46:06.000','red')('2020-08-21 18:46:06.000','green'); + +SELECT color, toDateTime(timestamp) AS second +FROM order_test1 +GROUP BY color, second +ORDER BY color ASC, second DESC; + +select ''; +select ''; + +SELECT color, timestamp +FROM order_test1 +GROUP BY color, timestamp +ORDER BY color ASC, timestamp DESC; + +select ''; +select '------cast to String----'; +select ''; + +SELECT cast(color,'String') color, toDateTime(timestamp) AS second +FROM order_test1 +GROUP BY color, second +ORDER BY color ASC, second DESC; + +select ''; +select ''; + +SELECT cast(color,'String') color, timestamp +FROM order_test1 +GROUP BY color, timestamp +ORDER BY color ASC, timestamp DESC;