Repository navigation
Preserve SettingFieldMaxThreads::is_auto across SET <name> = DEFAULT - #103991
alexey-milovidov merged 4 commits into
Conversation
`BaseSettings::resetValueToDefault` previously round-tripped the default value through `static_cast<Field>(*default_setting)` followed by `operator=(const Field &)` on the destination. For `SettingFieldMaxThreads`, this round-trip is not invertible: `operator Field()` returns the resolved auto-value (e.g. `32`), so applying it back via `operator=(const Field &)` reaches `fieldToMaxThreads(Field(32))` -> `operator=(UInt64 32)` and clears `is_auto`. After `SET max_threads = DEFAULT`, `system.settings` would report `32` instead of `'auto(32)'`. The same bug affected `max_final_threads` and `max_parsing_threads`. Replace the lossy Field round-trip with a per-setting `reset_function` lambda that performs typed copy assignment via the setting's own `operator=`: `data.NAME = SettingField##TYPE(DEFAULT);` followed by `setChanged(false)`. Typed assignment preserves all type-specific state (`is_auto`, plus any future internal flags) and is correct for every existing `SettingField` type. The fix is structural - it removes the entire class of non-invertible-Field bugs rather than patching a single symptom. The new test `tests/queries/0_stateless/04202_set_default_preserves_max_threads_auto.sql` covers the three affected `SettingFieldMaxThreads` settings, the single-setting and multi-setting forms of `SET ... = DEFAULT`, idempotent reset, and control assertions for `SettingAutoWrapper` and a regular numeric setting. Closes ClickHouse#103120
|
Pre-PR validation gate (per task-worker policy): (a) Deterministic repro? Yes. On master HEAD SET max_threads = 4;
SET max_threads = DEFAULT;
SELECT value FROM system.settings WHERE name = 'max_threads';
-- master: 32 <- bug: is_auto lost
-- patched: 'auto(32)' <- fixedReproduces 100% of the time on the reporter's exact reproducer. (b) Root cause explained? Yes. (c) Fix matches root cause? Yes. The fix replaces the lossy (d) Test intent preserved? N/A - this is a brand-new test. It is robust against CI session-level randomization (it starts by resetting all three settings to DEFAULT) and asserts both the auto state of the (e) Both directions demonstrated?
(f) General, not narrow patch? Yes. The change is to the macro that emits Session: |
|
@Algunenano - PR is up per your request on #103120. Ready for review. cc @vitlibar as the other listed expert for |
|
Workflow [PR], commit [eb131f0] Summary: ✅ AI ReviewSummaryThis PR fixes loss of ClickHouse Rules
Final VerdictStatus: ✅ Approve |
Per @Algunenano's review feedback on PR ClickHouse#103991, replace the per-setting `reset_function` lambda with a structurally smaller fix: have `SettingFieldMaxThreads::operator Field` return the canonical `"auto"` keyword when `is_auto` is set, mirroring `SettingAutoWrapper::operator Field`. This makes the `Field` round-trip in `BaseSettings::resetValueToDefault` (and any other code path that round-trips through `Field`) invertible: `fieldToMaxThreads(Field("auto"))` parses back to the canonical 0, and `SettingFieldMaxThreads::operator=(UInt64(0))` sets `is_auto = true` and recomputes `value = getAuto()`. No changes to `BaseSettings.h` are needed. Why this is correct: * `SettingAutoWrapper` already uses this pattern for its own auto/keyword duality, so the round-trip is invertible there. The bug existed only for `SettingFieldMaxThreads`, which is a separate type. * `writeBinary` already canonicalizes to 0 (`writeVarUInt(is_auto ? 0 : value)`), so the binary serialization path was already invertible. The fix aligns the Field path with the binary path. * `system.settings` reads via `getValueString` -> `toString`, which still prints `'auto(N)'` and is therefore unaffected. * Distributed forwarding uses `STRINGS_WITH_FLAGS` (calls `toString`) or the legacy binary format (calls `writeBinary`); neither uses `operator Field`. Why this is smaller / cheaper than the previous lambda-based fix: * Reverts the per-setting `reset_function` lambda generated by the `IMPLEMENT_SETTINGS_TRAITS_` macros — no extra code generated for the ~1000+ settings across `Settings`, `MergeTreeSettings`, `KafkaSettings`, etc. * Removes the extra `reset_function` field from `FieldInfo`. * Net diff: 1 functional line in `SettingsFields.h` (plus comment). Verified locally: 50/50 passes with full CI randomization on the regression test, plus all related stateless tests (`02995_new_settings_history`, `02992_settings_overflow`, `00282_settings_default`, all `max_threads` stateless tests). Manual repro of the bug now restores `'auto(N)'` after `SET max_threads = DEFAULT` for all three `MaxThreads` settings. Closes ClickHouse#103120 (alongside the prior commit on this PR).
This replaces the previous attempt that made `operator Field` return
`Field("auto")` for the auto state. That approach was correct in spirit
but backward-incompatible: `SettingFieldMaxThreads` is documented as
behaving like a regular `UInt64` for compatibility, and code reading
`getSetting('max_threads')` as numeric would observe a String type
when the setting is in the default state.
Reverts `operator Field` to its master form (`return value;`).
Instead, override `SettingFieldMaxThreads::setChanged(bool)` to restore
the canonical default state (`is_auto = true; value = getAuto();`) when
called with `false`. `BaseSettings::resetValueToDefault`
(`src/Core/BaseSettings.h:1151`) is the only caller of `setChanged(false)`
in the tree -- a `grep -rn 'setChanged\s*(' src/ programs/ base/`
confirms zero other call sites -- so the hook only fires on the
`SET <name> = DEFAULT` path that has the bug. No per-setting lambdas,
no `FieldInfo` enlargement, no public-API change.
Per @Algunenano's directive on the inline review thread of this PR.
Issue: ClickHouse#103120
| changed = changed_; | ||
| if (!changed_) | ||
| { | ||
| is_auto = true; |
There was a problem hiding this comment.
setChanged(false) is used for all SettingFieldMaxThreads settings, but this override always restores auto (is_auto = true; value = getAuto()). That silently changes semantics for settings whose default is an explicit number, not auto.
Concrete example in this PR: max_download_threads is declared as DECLARE(MaxThreads, max_download_threads, 4, ...) in src/Core/Settings.cpp. With this change:
SET max_download_threads = 1;SET max_download_threads = DEFAULT;
will reset to auto(N) instead of the declared default 4. So the fix for max_threads regresses other MaxThreads settings.
Could we restore the typed default value here (or in resetValueToDefault) instead of hard-coding the auto state?
There was a problem hiding this comment.
Replaced the setChanged hook with a typed-copy override of a new SettingFieldBase::resetFromDefault virtual in eb131f01. setChanged is now byte-identical to master (just changed = changed_;); max_download_threads is preserved because typed copy reproduces the declared default member-wise, including is_auto = false, value = 4.
Test extended to lock the spec for both default kinds: a max_download_threads = 0 (switches to auto) -> SET max_download_threads = DEFAULT (must restore 4, not stay auto) sequence catches exactly this regression class.
|
@groeneai We need a comprehensive test of how thing should work because the AI reviewer keeps finding corner cases. Let's add tests for other settings, include checks with |
…ads::is_auto Previous attempts: - 15ad0c6: per-setting reset_function lambda - rejected by reviewer (build/code bloat) - fc9a691: operator Field returned String("auto") - rejected by AI reviewer (changes the Field type observed by getSetting('max_threads') from numeric to string) - 985be25: setChanged(false) hard-coded is_auto = true - rejected by AI reviewer because max_download_threads is also a SettingFieldMaxThreads with declared default 4 (NOT auto), so the hard-coded auto-restoration regressed it to 'auto(N)' on SET = DEFAULT This iteration takes the AI reviewer's "restore the typed default value" recommendation literally, by routing the reset through a typed copy instead of a Field round-trip: - Add `SettingFieldBase::resetFromDefault(const SettingFieldBase &)`. Default implementation matches the previous Field round-trip (`*this = static_cast<Field>(other); setChanged(false);`), so every type whose `operator Field` is invertible keeps its current behaviour and no per-setting code is generated. - Override in `SettingFieldMaxThreads` to copy member-wise via the existing typed `operator=(const SettingFieldMaxThreads &)`. This preserves whichever state the typed default carries: `is_auto = true, value = getAuto()` for settings declared with default `0` (`max_threads`, `max_final_threads`, `max_parsing_threads`), and `is_auto = false, value = 4` for `max_download_threads`. No special-casing or hard-coded auto state. - `BaseSettings::resetValueToDefault` dispatches through the new virtual instead of inlining the Field round-trip, so each type chooses for itself. `operator Field` is unchanged (still `return value`), so `getSetting('max_threads')` keeps returning a numeric `Field` and there is no backward-incompatible change for callers that treat `max_threads` as `UInt64`. The diff in `BaseSettings.h` is one line of behaviour change (no per-setting codegen), and `SettingsFields.h` adds one virtual method on the base and one override on `SettingFieldMaxThreads`. Closes ClickHouse#103120. Comprehensive regression test in `04202_set_default_preserves_max_threads_auto.sql` covers all four `SettingFieldMaxThreads` settings with their distinct declared defaults (auto-default and explicit-default groups), the multi-setting `SET a = DEFAULT, b = DEFAULT` form, idempotent resets, the `0` <-> auto transition for `max_download_threads`, `getSetting` return-type checks (`UInt`-family), and `SettingAutoWrapper` / `SettingFieldNumber` controls. 50/50 passes locally with full CI randomization. Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
@Algunenano done in Comprehensive test ( Now covers every
50/50 passes locally with full CI randomization. Fix ( Reverted commit struct SettingFieldBase
{
// ... existing virtuals ...
/// Default impl: Field round-trip + setChanged(false). Suits any type whose `operator Field`
/// is invertible. Override in types where the round-trip would lose state.
virtual void resetFromDefault(const SettingFieldBase & default_value)
{
*this = static_cast<Field>(default_value);
setChanged(false);
}
};
struct SettingFieldMaxThreads final : SettingFieldBase
{
// setChanged is back to plain `changed = changed_;` (byte-identical to master).
void resetFromDefault(const SettingFieldBase & default_value) override
{
*this = static_cast<const SettingFieldMaxThreads &>(default_value);
changed = false;
}
};
Why this addresses every concern raised so far on this PR:
cc @vitlibar. |
|
Pre-PR validation gate (per task-worker policy, against (a) Deterministic repro? Yes. On master HEAD SET max_threads = 4;
SET max_threads = DEFAULT;
SELECT value FROM system.settings WHERE name = 'max_threads';
-- master: '4' (BUG, drops is_auto)
-- with fix: ''auto(N)'' (correct)
SET max_download_threads = 0;
SET max_download_threads = DEFAULT;
SELECT value FROM system.settings WHERE name = 'max_download_threads';
-- master: '4' (correct)
-- broken setChanged hook (985be25a): ''auto(N)'' (REGRESSION)
-- with fix: '4' (correct)(b) Root cause explained? Yes. (c) Fix matches root cause? Yes. The lossy step is the (d) Test intent preserved? / New tests added? New comprehensive test
(e) Both directions demonstrated? Yes. Test passes 50/50 with full CI randomization on the fixed build ( (f) Fix is general, not a narrow patch? Yes. The audit of every |
LLVM Coverage Report
Changed lines: 93.75% (15/16) | lost baseline coverage: 1 line(s) · Uncovered code |
Master added `SettingFieldBase::resetFromDefault` (issue ClickHouse#103120, PR ClickHouse#103991) to preserve `SettingFieldMaxThreads::is_auto` across `SET <name> = DEFAULT`, which the previous `Field` round-trip in `BaseSettings::resetValueToDefault` dropped because `SettingFieldMaxThreads::operator Field` is non-invertible. This branch removed `SettingFieldBase` entirely, so the override mechanism doesn't apply. Equivalent fix without virtuals: keep one default-constructed `Data` instance per `Accessor` (no per-setting cost) and reset individual settings via a per-type `typed_copy` function pointer in `SettingFieldOps` that does `*static_cast<T *>(dst) = *static_cast<const T *>(src)`. The defaulted copy assignment of `SettingFieldMaxThreads` preserves all members including `is_auto`, so the regression test `04202_set_default_preserves_max_threads_auto` passes without any per-setting code. The cached default `Data` also lets `getDefaultValueString` read directly through the existing `to_string` op, replacing the per-setting `getSettingDefault_` switch and the `default_fn_` / `default_index_` fields in `FieldInfo` (16 B × ~2k settings ≈ 32 KB removed from the `Settings` accessor singleton). Also rename the `sf*` short helpers in `SettingsFields.h` to spelled-out `settingField*` names.
Fixes #103120.
SET max_threads = DEFAULT(and the same formax_final_threadsandmax_parsing_threads) used to lose theis_autoflag forSettingFieldMaxThreads-typed settings. After the reset,system.settingswould report32instead of'auto(32)', and the runtime would behave as if the user had explicitly pinnedmax_threadsto the resolved core count.Root cause
BaseSettings::resetValueToDefault(src/Core/BaseSettings.h) round-tripped the default value throughField:For
SettingFieldMaxThreads,operator Field()returns the resolved auto-value (thevaluemember, e.g.32), not the canonical0that the constructor maps back to auto. The destination'soperator=(const Field &)then callsfieldToMaxThreads(Field(32)) -> operator=(UInt64 32) -> is_auto = !32 = false. TheFieldround-trip is not invertible, and the auto state is permanently lost.Fix
Replace the lossy
Fieldround-trip with a per-settingreset_functionlambda generated by the same macro that emitsget_data_function/create_default_function:[](Data & data) { data.NAME = SettingField##TYPE(DEFAULT); data.NAME.setChanged(false); }Typed copy assignment goes through the setting type's own
operator=, so all type-specific state is preserved (is_autoforSettingFieldMaxThreads, plus any future internal flags on other types). The change is structural - it eliminates the entire class of non-invertible-Fieldreset bugs rather than patching a single symptom in one type.The change touches only the macro that builds
FieldInfoand theresetValueToDefaultaccessor, so it applies uniformly to every settings group (regularSettings,MergeTreeSettings,KafkaSettings, etc.).Test
tests/queries/0_stateless/04202_set_default_preserves_max_threads_auto.sqlcovers:SET <name> = DEFAULTfor all threeSettingFieldMaxThreadssettings,SET a = DEFAULT, b = DEFAULT, ...form,SettingAutoWrappercontrol (query_plan_join_swap_table) that must remain correct,max_block_size) for thechangedflag.The test starts by resetting the three settings to
DEFAULTso it is robust against the test runner's session-level randomization. On a buggy build the test fails on the first three assertions; on the patched build it passes (verified locally with--test-runs 50under full CI randomization).cc @Algunenano - per your request on #103120.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
SET max_threads = DEFAULT(and the same formax_final_threadsandmax_parsing_threads) no longer loses the auto state. Previously, after resetting one of these settings,system.settingswould report the resolved core count (e.g.32) instead of'auto(32)', and the server would behave as if the value had been explicitly pinned.Documentation entry for user-facing changes