| 186 | } |
| 187 | |
| 188 | void compileSortDescriptionIfNeeded(SortDescription & description, const DataTypes & sort_description_types, bool increase_compile_attempts) |
| 189 | { |
| 190 | static std::unordered_map<UInt128, UInt64, UInt128Hash> counter; |
| 191 | static std::mutex mutex; |
| 192 | |
| 193 | if (!description.compile_sort_description || sort_description_types.empty()) |
| 194 | return; |
| 195 | |
| 196 | for (size_t i = 0; i < description.size(); ++i) |
| 197 | { |
| 198 | auto nested_type = removeNullable(sort_description_types[i]); |
| 199 | if (!sort_description_types[i]->createColumn()->isComparatorCompilable() || |
| 200 | (!canBeNativeType(*sort_description_types[i]) && !WhichDataType(nested_type).isString() && !WhichDataType(nested_type).isFixedString())) |
| 201 | return; |
| 202 | |
| 203 | /// JIT comparator does not support collation-aware comparison |
| 204 | if (description[i].collator) |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | auto description_dump = getSortDescriptionDump(description, sort_description_types); |
| 210 | |
| 211 | SipHash sort_description_dump_hash; |
| 212 | sort_description_dump_hash.update(description_dump); |
| 213 | |
| 214 | const auto sort_description_hash_key = sort_description_dump_hash.get128(); |
| 215 | |
| 216 | { |
| 217 | std::lock_guard lock(mutex); |
| 218 | UInt64 & current_counter = counter[sort_description_hash_key]; |
| 219 | if (current_counter < description.min_count_to_compile_sort_description) |
| 220 | { |
| 221 | current_counter += static_cast<UInt64>(increase_compile_attempts); |
| 222 | return; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | std::shared_ptr<CompiledSortDescriptionFunctionHolder> compiled_sort_description_holder; |
| 227 | |
| 228 | if (auto * compilation_cache = CompiledExpressionCacheFactory::instance().tryGetCache()) |
| 229 | { |
| 230 | auto [compiled_function_cache_entry, _] = compilation_cache->getOrSet(sort_description_hash_key, [&] () |
| 231 | { |
| 232 | LOG_TRACE(getLogger(), "Compile sort description {}", description_dump); |
| 233 | |
| 234 | auto jit_owner = getJITInstancePtr(); |
| 235 | auto compiled_sort_description = compileSortDescription(*jit_owner, description, sort_description_types, description_dump); |
| 236 | return std::make_shared<CompiledSortDescriptionFunctionHolder>(std::move(compiled_sort_description), std::move(jit_owner)); |
| 237 | }); |
| 238 | |
| 239 | compiled_sort_description_holder = std::static_pointer_cast<CompiledSortDescriptionFunctionHolder>(compiled_function_cache_entry); |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | LOG_TRACE(getLogger(), "Compile sort description {}", description_dump); |
| 244 | auto jit_owner = getJITInstancePtr(); |
| 245 | auto compiled_sort_description = compileSortDescription(*jit_owner, description, sort_description_types, description_dump); |
no test coverage detected