| 106 | } |
| 107 | |
| 108 | void getBlockSortPermutationImpl(const Block & block, const SortDescription & description, IColumn::PermutationSortStability stability, UInt64 limit, IColumn::Permutation & permutation) |
| 109 | { |
| 110 | if (!block) |
| 111 | return; |
| 112 | |
| 113 | ColumnsWithSortDescriptions columns_with_sort_descriptions = getColumnsWithSortDescription(block, description); |
| 114 | |
| 115 | bool all_const = true; |
| 116 | for (const auto & column : columns_with_sort_descriptions) |
| 117 | { |
| 118 | if (!column.column_const) |
| 119 | { |
| 120 | all_const = false; |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (unlikely(all_const)) |
| 126 | return; |
| 127 | |
| 128 | /// If only one column to sort by |
| 129 | if (columns_with_sort_descriptions.size() == 1) |
| 130 | { |
| 131 | auto & column_with_sort_description = columns_with_sort_descriptions[0]; |
| 132 | |
| 133 | IColumn::PermutationSortDirection direction = column_with_sort_description.description.direction == -1 ? IColumn::PermutationSortDirection::Descending : IColumn::PermutationSortDirection::Ascending; |
| 134 | int nan_direction_hint = column_with_sort_description.description.nulls_direction; |
| 135 | const auto & column = column_with_sort_description.column; |
| 136 | |
| 137 | if (isCollationRequired(column_with_sort_description.description)) |
| 138 | column->getPermutationWithCollation( |
| 139 | *column_with_sort_description.description.collator, direction, stability, limit, nan_direction_hint, permutation); |
| 140 | else |
| 141 | column->getPermutation(direction, stability, limit, nan_direction_hint, permutation); |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | size_t size = block.rows(); |
| 146 | permutation.resize(size); |
| 147 | for (size_t i = 0; i < size; ++i) |
| 148 | permutation[i] = i; |
| 149 | |
| 150 | if (limit >= size) |
| 151 | limit = 0; |
| 152 | |
| 153 | EqualRanges ranges; |
| 154 | ranges.emplace_back(0, permutation.size()); |
| 155 | |
| 156 | for (const auto & column_with_sort_description : columns_with_sort_descriptions) |
| 157 | { |
| 158 | while (!ranges.empty() && limit && limit <= ranges.back().first) |
| 159 | ranges.pop_back(); |
| 160 | |
| 161 | if (ranges.empty()) |
| 162 | break; |
| 163 | |
| 164 | if (column_with_sort_description.column_const) |
| 165 | continue; |
no test coverage detected