@@ -41,14 +41,6 @@ struct SortCursorImpl {
4141 size_t pos = 0 ;
4242 size_t rows = 0 ;
4343
44- /* * Determines order if comparing columns are equal.
45- * Order is determined by number of cursor.
46- *
47- * Cursor number (always?) equals to number of merging part.
48- * Therefore this field can be used to determine part number of current row (see ColumnGathererStream).
49- */
50- size_t order = 0 ;
51-
5244 using NeedCollationFlags = std::vector<UInt8>;
5345
5446 /* * Should we use Collator to sort a column? */
@@ -59,18 +51,16 @@ struct SortCursorImpl {
5951
6052 SortCursorImpl () = default ;
6153
62- SortCursorImpl (const Block& block, const SortDescription& desc_, size_t order_ = 0 )
54+ SortCursorImpl (const Block& block, const SortDescription& desc_)
6355 : desc(desc_),
6456 sort_columns_size (desc.size()),
65- order(order_),
6657 need_collation(desc.size()) {
6758 reset (block);
6859 }
6960
70- SortCursorImpl (const Columns& columns, const SortDescription& desc_, size_t order_ = 0 )
61+ SortCursorImpl (const Columns& columns, const SortDescription& desc_)
7162 : desc(desc_),
7263 sort_columns_size(desc.size()),
73- order(order_),
7464 need_collation(desc.size()) {
7565 for (auto & column_desc : desc) {
7666 if (!column_desc.column_name .empty ()) {
@@ -101,9 +91,6 @@ struct SortCursorImpl {
10191 ? block.get_position_by_name (column_desc.column_name )
10292 : column_desc.column_number ;
10393 sort_columns.push_back (columns[column_number].get ());
104-
105- // need_collation[j] = desc[j].collator != nullptr && typeid_cast<const ColumnString *>(sort_columns.back()); /// TODO Nullable(String)
106- // has_collation |= need_collation[j];
10794 }
10895
10996 pos = 0 ;
@@ -175,84 +162,67 @@ struct SortCursor {
175162 const SortCursorImpl* operator ->() const { return impl; }
176163
177164 // / The specified row of this cursor is greater than the specified row of another cursor.
178- bool greater_at (const SortCursor& rhs, size_t lhs_pos, size_t rhs_pos) const {
165+ int8_t greater_at (const SortCursor& rhs, size_t lhs_pos, size_t rhs_pos) const {
179166 for (size_t i = 0 ; i < impl->sort_columns_size ; ++i) {
180167 int direction = impl->desc [i].direction ;
181168 int nulls_direction = impl->desc [i].nulls_direction ;
182169 int res = direction * impl->sort_columns [i]->compare_at (lhs_pos, rhs_pos,
183170 *(rhs.impl ->sort_columns [i]),
184171 nulls_direction);
185- if (res > 0 ) return true ;
186- if (res < 0 ) return false ;
172+ if (res > 0 ) return 1 ;
173+ if (res < 0 ) return - 1 ;
187174 }
188- return impl-> order > rhs. impl -> order ;
175+ return 0 ;
189176 }
190177
191178 // / Checks that all rows in the current block of this cursor are less than or equal to all the rows of the current block of another cursor.
192- bool totally_less_or_equals (const SortCursor& rhs) const {
179+ bool totally_less (const SortCursor& rhs) const {
193180 if (impl->rows == 0 || rhs.impl ->rows == 0 ) return false ;
194181
195182 // / The last row of this cursor is no larger than the first row of the another cursor.
196- return ! greater_at (rhs, impl->rows - 1 , 0 );
183+ return greater_at (rhs, impl->rows - 1 , 0 ) == - 1 ;
197184 }
198185
199- bool greater (const SortCursor& rhs) const { return greater_at (rhs, impl->pos , rhs.impl ->pos ); }
186+ bool greater (const SortCursor& rhs) const { return greater_at (rhs, impl->pos , rhs.impl ->pos ) > 0 ; }
200187
201188 // / Inverted so that the priority queue elements are removed in ascending order.
202189 bool operator <(const SortCursor& rhs) const { return greater (rhs); }
203190};
204191
205- // / Separate comparator for locale-sensitive string comparisons
206- // struct SortCursorWithCollation
207- // {
208- // SortCursorImpl * impl;
209- //
210- // SortCursorWithCollation(SortCursorImpl * impl_) : impl(impl_) {}
211- // SortCursorImpl * operator-> () { return impl; }
212- // const SortCursorImpl * operator-> () const { return impl; }
213- //
214- // bool greater_at(const SortCursorWithCollation & rhs, size_t lhs_pos, size_t rhs_pos) const
215- // {
216- // for (size_t i = 0; i < impl->sort_columns_size; ++i)
217- // {
218- // int direction = impl->desc[i].direction;
219- // int nulls_direction = impl->desc[i].nulls_direction;
220- // int res;
221- // if (impl->need_collation[i])
222- // {
223- // const ColumnString & column_string = assert_cast<const ColumnString &>(*impl->sort_columns[i]);
224- // res = column_string.compare_at_with_collation(lhs_pos, rhs_pos, *(rhs.impl->sort_columns[i]), *impl->desc[i].collator);
225- // }
226- // else
227- // res = impl->sort_columns[i]->compare_at(lhs_pos, rhs_pos, *(rhs.impl->sort_columns[i]), nulls_direction);
228- //
229- // res *= direction;
230- // if (res > 0)
231- // return true;
232- // if (res < 0)
233- // return false;
234- // }
235- // return impl->order > rhs.impl->order;
236- // }
237- //
238- // bool totally_less_or_equals(const SortCursorWithCollation & rhs) const
239- // {
240- // if (impl->rows == 0 || rhs.impl->rows == 0)
241- // return false;
242- //
243- // /// The last row of this cursor is no larger than the first row of the another cursor.
244- // return !greater_at(rhs, impl->rows - 1, 0);
245- // }
246- //
247- // bool greater(const SortCursorWithCollation & rhs) const
248- // {
249- // return greater_at(rhs, impl->pos, rhs.impl->pos);
250- // }
251- //
252- // bool operator< (const SortCursorWithCollation & rhs) const
253- // {
254- // return greater(rhs);
255- // }
256- // };
192+ // / For easy copying.
193+ struct SortBlockCursor {
194+ SortCursorImpl* impl;
195+
196+ SortBlockCursor (SortCursorImpl* impl_) : impl(impl_) {}
197+ SortCursorImpl* operator ->() { return impl; }
198+ const SortCursorImpl* operator ->() const { return impl; }
199+
200+ // ~SortBlockCursor() { delete impl;}
201+
202+ // / The specified row of this cursor is greater than the specified row of another cursor.
203+ int8_t less_at (const SortBlockCursor& rhs, int rows) const {
204+ for (size_t i = 0 ; i < impl->sort_columns_size ; ++i) {
205+ int direction = impl->desc [i].direction ;
206+ int nulls_direction = impl->desc [i].nulls_direction ;
207+ int res = direction * impl->sort_columns [i]->compare_at (rows, rhs->rows - 1 ,
208+ *(rhs.impl ->sort_columns [i]),
209+ nulls_direction);
210+ if (res < 0 ) return 1 ;
211+ if (res > 0 ) return -1 ;
212+ }
213+ return 0 ;
214+ }
215+
216+ // / Checks that all rows in the current block of this cursor are less than or equal to all the rows of the current block of another cursor.
217+ bool totally_greater (const SortBlockCursor& rhs) const {
218+ if (impl->rows == 0 || rhs.impl ->rows == 0 ) return false ;
219+
220+ // / The last row of this cursor is no larger than the first row of the another cursor.
221+ return less_at (rhs, 0 ) == -1 ;
222+ }
223+
224+ // / Inverted so that the priority queue elements are removed in ascending order.
225+ bool operator <(const SortBlockCursor& rhs) const { return less_at (rhs, impl->rows - 1 ) >= 0 ; }
226+ };
257227
258228} // namespace doris::vectorized
0 commit comments