Repository navigation
Merge Fan-In - #121955
Merge Fan-In#121955nihalzp wants to merge 30 commits into
Conversation
|
Workflow [PR], commit [6555451] Summary: ❌
AI ReviewSummaryThis PR adds Findings
Final VerdictNeeds changes before merge: bump the query-plan / compatibility release markers for |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes172 object files changed (+387.09 KiB total), 1 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units16349 translation units recompiled, 34720 s compile time in total, 15841 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.05 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
|
| /// First global query-plan version that writes `max_external_merge_fan_in` in the step settings. | ||
| /// Zero omits the setting and preserves unlimited merging. Nonzero values require a reader that | ||
| /// knows the setting; older readers reject the unknown name before reading its value. | ||
| static constexpr auto DBMS_MIN_QUERY_PLAN_SERIALIZATION_VERSION_WITH_EXTERNAL_MERGE_FAN_IN = 20; |
There was a problem hiding this comment.
QueryPlanSerializationSettings::readBinary throws on unknown setting names, so keying this on the already-shipped global plan version 20 makes new 26.10.x writers incompatible with older 26.10.x readers as soon as they serialize max_external_merge_fan_in. With the new default 64, SortingStep::Settings::updatePlanSettings and DistinctStep::Settings::updatePlanSettings emit the new name by default, and JoinStepLogical::serializeSettings now forwards the same sort settings for every serialized Join step, even when the selected join algorithm never uses full_sorting_merge. That turns mixed-patch rollouts into UNKNOWN_SETTING failures unless operators pin compatibility = '26.9' or max_external_merge_fan_in = 0 cluster-wide.
Can we keep this setting off the version-20 wire format by default (for example by deriving it from the worker's local query settings, or by postponing wire serialization until the next global plan version) instead of keying it off >= 20?
There was a problem hiding this comment.
Version 20 has not shipped. 26.9 announces 19, both in v26.9.1.1629-stable and at the tip of the 26.9 branch, and 26.10 has no release branch or stable tag yet. So once this is in 26.10, every 26.10.x reader knows max_external_merge_fan_in, and a mixed-patch 26.10.x rollout cannot hit UNKNOWN_SETTING. The mechanism is right: QueryPlanSerializationSettings::readBinary throws on an unknown name, and the default 64 writes it. It only matters between two development builds of the same cycle, the same as for BlocksMarshalling and legacy_join_size_limits_trigger_spilling, which also use 20.
Using 20 is what the rules above DBMS_QUERY_PLAN_SERIALIZATION_VERSION ask for: the version moves at most once per release, and a new plan setting name reuses the value of the current release. Postponing the setting to the next global version would mean either a second bump in 26.10 or leaving it out of 26.10 plans entirely. Deriving it from the worker's local query settings would split the spill settings between the plan and the worker context, while max_bytes_before_external_sort and min_free_disk_space_for_temporary_data travel with the plan. If the PR misses the 26.10 cut, I will bump the version to 21 for 26.11. I have also listed the setting under version 20 in 99f15d898d4.
There was a problem hiding this comment.
I guess now we need to increment the version
…rge-fan-in Resolve the conflict with the move of the settings changes history into the setting declarations (ee6b44f): the history records this PR added to `SettingsChangesHistory.cpp` now live as trailing arguments of the `DECLARE` of each setting. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…selection` as long
…ut of the shared helper
|
📊 Cloud Performance Report ✅ AI verdict: No query change is attributed to this PR. One query was flagged: clickbench Q33 ran -5.6% faster (975 ms vs 1033 ms), but that falls inside master's normal range for this query. The query's history is noisy, and CPU time barely moved, so it stays at no_change. Most of the PR changes code that only runs when sorting, DISTINCT or partial-merge joins spill to disk, which these benchmark queries don't do. The pipeline executor change only alters where completed processors are released and adds profiling for them, which shouldn't change query speed. clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
|
Dear @azat, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
| /// First global query-plan version that writes `max_external_merge_fan_in` in the step settings. | ||
| /// Zero omits the setting and preserves unlimited merging. Nonzero values require a reader that | ||
| /// knows the setting; older readers reject the unknown name before reading its value. | ||
| static constexpr auto DBMS_MIN_QUERY_PLAN_SERIALIZATION_VERSION_WITH_EXTERNAL_MERGE_FAN_IN = 20; |
There was a problem hiding this comment.
I guess now we need to increment the version
| DECLARE(UInt64, max_external_merge_fan_in, 64, R"( | ||
| Limits the number of temporary files merged simultaneously for external sorting, including sorting for `full_sorting_merge` joins, and external `DISTINCT`. Any remaining in-memory run joins only the final merge and does not count toward this limit. Larger collections are reduced through intermediate merges on disk, which combine the smallest files first to minimize the amount of data rewritten. | ||
|
|
||
| Smaller nonzero values reduce the number of active readers but can require more disk I/O. This setting does not change the spill threshold or bound total query memory. The `partial_merge` join algorithm uses `join_on_disk_max_files_to_merge` instead. |
There was a problem hiding this comment.
Do we really need to include "The partial_merge join algorithm uses join_on_disk_max_files_to_merge instead." here?
| return std::make_shared<MergingSortedTransform>( | ||
| header, headers.size(), description, block_size, /*max_block_size_bytes=*/ 0, | ||
| /*max_dynamic_subcolumns=*/ std::nullopt, SortingQueueStrategy::Default); | ||
| }; |
There was a problem hiding this comment.
Do we need this lambda here? Can we inline it into "final_merge"?
| } | ||
|
|
||
| return flushToManyFiles(tmp_data, std::move(pipeline), callback); | ||
| if (headers.size() > 1) |
There was a problem hiding this comment.
This check looks odd, maybe better to move this PE where we do spilling?
|
|
||
| /// Completed processors no longer appear in the pipeline's final profile. Log | ||
| /// them before releasing their buffers, outside the graph's preparation locks. | ||
| if (profile_processors && !removed.empty()) |
There was a problem hiding this comment.
"logProcessorProfile" already has a check for "profile_processors", so maybe we can remove this one, and cleanup everything later
| SharedHeader header, std::vector<Group> groups_, SourcePtr tail_, MergeFactory final_merge_, size_t max_fan_in_, | ||
| TemporaryDataOnDiskScopePtr tmp_data_, size_t min_free_disk_space_, LoggerPtr log_); | ||
|
|
||
| static size_t validateFanIn(size_t fan_in); |
There was a problem hiding this comment.
Maybe better to move this into settings serialization?
|
|
||
| @pytest.mark.parametrize("operation", ["sort", "distinct"]) | ||
| @pytest.mark.parametrize("fan_in", [0, 2]) | ||
| def test_external_merge_removed_processor_profiles(start_cluster, operation, fan_in): |
There was a problem hiding this comment.
Can we convert this into a regular test? I don't see a reason for keeping it integration
| /// Accumulate sorted input chunks in memory. When the configured spill thresholds are exceeded, | ||
| /// merge the buffered chunks into a sorted stream and write it to a temporary file. At the end of | ||
| /// input, `ExternalMergeSource` reduces excess files through intermediate merges, then merges the | ||
| /// remaining files with the chunks still in memory. Without spill files, merge only in memory. |
There was a problem hiding this comment.
I like the old style comment more, maybe we can just update it to include "ExternalMergeSource"?
| /// can retain different column layouts and duplicate-handling rules. The limit applies to file readers | ||
| /// owned by this coordinator; it does not bound total query memory or readers in other coordinators. |
There was a problem hiding this comment.
| /// can retain different column layouts and duplicate-handling rules. The limit applies to file readers | |
| /// owned by this coordinator; it does not bound total query memory or readers in other coordinators. | |
| /// can retain different column layouts and duplicate-handling rules. |
This looks redundant
|
|
||
| Possible values are `0` for unlimited fan-in, or integers starting from `2`. With `0`, all temporary files enter the final merge directly, without intermediate merges. | ||
| )", 0, \ | ||
| {"26.10", 0, 64, "New setting to limit the number of temporary files merged simultaneously for external sorting, including sorting for `full_sorting_merge` JOINs, and external `DISTINCT`. Any remaining in-memory run joins only the final merge and does not count toward this limit. A value of `0` preserves unlimited fan-in."}) \ |
There was a problem hiding this comment.
max_external_merge_fan_in is still recorded here as a 26.10 setting, but 26.10 is already cut (there is a 26.10 release branch and a v26.10.2.103-stable tag), so this PR can now only ship first in 26.11. If this entry stays at 26.10, then compatibility = '26.10' on a future build will still derive max_external_merge_fan_in = 64, even though real 26.10 builds never had bounded fan-in enabled by default. Can we move this history entry to the first release that can actually contain the feature, together with the query-plan version bump, and update the serialization test that currently uses compatibility = '26.9'?
Spilling reduces memory while collecting rows, but merging thousands of temporary files can bring the memory usage back up. External sorting and external
DISTINCTopen every spill file for the final merge. Each reader needs buffers and a decoded block, so memory grows with the number of files.full_sorting_mergejoins have the same problem when sorting their inputs.Add a shared
ExternalMergeSourcethat reduces the file count through intermediate merges. Each merge reads a bounded group of files and writes one replacement file. It chooses the smallest files by compressed size, then puts the replacement back into the selection. This reduces unnecessary rewriting of large files. Only enough files are merged to bring the final merge within the limit.max_external_merge_fan_indefaults to64;0keeps unlimited fan-in. The setting applies to external sorting, externalDISTINCT, and sorting forfull_sorting_mergejoins.partial_mergejoins already bounded fan-in; they now use the shared implementation with their existingjoin_on_disk_max_files_to_mergesetting.External sort
Sort 81,920,000 keys in 4,096-row chunks. Each chunk becomes one initial file.
External
DISTINCTFind distinct keys among 81,920,000 rows. The threshold forces spilling from the first chunk, producing one initial file per 4,096 rows.
External join
Join two inputs of 40,960,000 rows with
full_sorting_merge. Each side produces 10,000 sorted files, for 20,000 initial files in total.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Bound the number of temporary files merged simultaneously for external sorting, external
DISTINCT, andfull_sorting_mergejoins with the newmax_external_merge_fan_insetting, which defaults to64. Intermediate merges choose the smallest compatible files first, reducing peak memory during merges at the cost of additional disk I/O. Set it to0to retain unlimited fan-in.partial_mergejoins share the merge implementation and retain their existingjoin_on_disk_max_files_to_mergesetting.Workflow [PR]
Sync PR [sync-upstream/pr/121955]