MCPcopy Create free account
hub / github.com/ClickHouse/ClickHouse / getInputOrderImpl

Method getInputOrderImpl

src/Storages/ReadInOrderOptimizer.cpp:203–285  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

201}
202
203InputOrderInfoPtr ReadInOrderOptimizer::getInputOrderImpl(
204 const StorageMetadataPtr & metadata_snapshot,
205 const SortDescription & description,
206 const ManyExpressionActions & actions,
207 const ContextPtr & context,
208 UInt64 limit) const
209{
210 const Names & sorting_key_columns = metadata_snapshot->getSortingKeyColumns();
211 /// read_direction will be set from the first non-constant ORDER BY column
212 int read_direction = 0;
213
214 auto fixed_sorting_columns = getFixedSortingColumns(query, sorting_key_columns, context);
215
216 SortDescription sort_description_for_merging;
217 sort_description_for_merging.reserve(description.size());
218
219 size_t desc_pos = 0;
220 size_t key_pos = 0;
221
222 while (desc_pos < description.size() && key_pos < sorting_key_columns.size())
223 {
224 if (forbidden_columns.contains(description[desc_pos].column_name))
225 break;
226
227 auto match = matchSortDescriptionAndKey(actions[desc_pos]->getActions(), description[desc_pos], sorting_key_columns[key_pos]);
228
229 /// If the ORDER BY column matches a fixed (constant) key column,
230 /// add it to the sort description but don't let it set read_direction.
231 /// Example: ORDER BY tenant, event_time DESC with WHERE tenant='42'
232 /// The 'tenant' column is constant, so read direction should come from event_time DESC.
233 if (match.direction && fixed_sorting_columns.contains(sorting_key_columns[key_pos]))
234 {
235 /// Still add to sort description - the column matches, it's just constant
236 sort_description_for_merging.push_back(description[desc_pos]);
237 ++desc_pos;
238 ++key_pos;
239 continue;
240 }
241
242 bool is_matched = match.direction && (read_direction == 0 || match.direction == read_direction);
243
244 if (!is_matched)
245 {
246 /// If one of the sorting columns is constant after filtering,
247 /// skip it, because it won't affect order anymore.
248 if (fixed_sorting_columns.contains(sorting_key_columns[key_pos]))
249 {
250 ++key_pos;
251 continue;
252 }
253
254 break;
255 }
256
257 if (read_direction == 0)
258 read_direction = match.direction;
259
260 sort_description_for_merging.push_back(description[desc_pos]);

Callers

nothing calls this directly

Calls 9

getFixedSortingColumnsFunction · 0.85
getSortingKeyColumnsMethod · 0.80
reserveMethod · 0.45
sizeMethod · 0.45
containsMethod · 0.45
getActionsMethod · 0.45
push_backMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected