Skip to content

Commit efb8f9e

Browse files
Port the value-less setting guards for raw engine-settings readers from #111994
Some engines read the `SETTINGS` clause of their definition directly from `ASTStorage::settings->changes` instead of applying it to a `BaseSettings` schema, so nothing rejected the value-less form `SETTINGS name` for them: `Join` silently turned `SETTINGS max_rows_in_join` into `max_rows_in_join = 1`, and the String readers (`disk`/`storage_policy` of the Log family, `keeper_path` of the queue engines) reported `BAD_GET` from `safeGet` at the wrong layer. Ported from `parser-standalone-v3` (#111994): * a `BaseSettingsHelpers::throwValuelessSettingIsNotBool` overload for callers that have no type name to report, and `SettingsChanges::tryGetChange` for consumers that also have to look at `shorthand`; * guards in `registerStorageJoin`, `getDiskName` of the Log family, and `createQueueStorage`; * `InterpreterSetQuery::applySettingsFromQuery` checks the shorthand before the constraint check converts the value, and moves the change to the context through `applySettingChange` so the `shorthand` flag is not dropped; * tests `04663_setting_boolean_shorthand_in_create_settings` and `04664_setting_boolean_shorthand_raw_engine_settings`. This makes #112067's containment of #111994 complete. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent be78590 commit efb8f9e

12 files changed

Lines changed: 98 additions & 3 deletions

‎src/Common/SettingsChanges.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ Field * SettingsChanges::tryGet(std::string_view name)
4646
return &change->value;
4747
}
4848

49+
const SettingChange * SettingsChanges::tryGetChange(std::string_view name) const
50+
{
51+
return find(*this, name);
52+
}
53+
54+
SettingChange * SettingsChanges::tryGetChange(std::string_view name)
55+
{
56+
return find(*this, name);
57+
}
58+
4959
bool SettingsChanges::insertSetting(std::string_view name, const Field & value)
5060
{
5161
auto it = std::find_if(begin(), end(), [&name](const SettingChange & change) { return change.name == name; });

‎src/Common/SettingsChanges.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class SettingsChanges : public std::vector<SettingChange>
3838
const Field * tryGet(std::string_view name) const;
3939
Field * tryGet(std::string_view name);
4040

41+
/// The whole change rather than its value, for consumers that also have to look at `shorthand`.
42+
const SettingChange * tryGetChange(std::string_view name) const;
43+
SettingChange * tryGetChange(std::string_view name);
44+
4145
/// Inserts element if doesn't exists and returns true, otherwise just returns false
4246
bool insertSetting(std::string_view name, const Field & value);
4347
/// Sets element to value, inserts if doesn't exist

‎src/Core/BaseSettings.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ void BaseSettingsHelpers::throwValuelessSettingIsNotBool(std::string_view name,
6464
String{name}, String{type}, String{name});
6565
}
6666

67+
void BaseSettingsHelpers::throwValuelessSettingIsNotBool(std::string_view name)
68+
{
69+
/// For consumers that read a `SettingChange` without a settings schema at hand, so they know the
70+
/// setting is not Bool but not what its type is.
71+
throw Exception(
72+
ErrorCodes::TYPE_MISMATCH,
73+
"Setting '{}' is not Bool, so it cannot be set without a value. Write '{} = <value>'",
74+
String{name}, String{name});
75+
}
76+
6777
/// Log the summary of unknown settings as a warning instead of warning for each one separately.
6878
void BaseSettingsHelpers::warningSettingNotFound(std::string_view name)
6979
{

‎src/Core/BaseSettings.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct BaseSettingsHelpers
3636
/// Error handling
3737
[[noreturn]] static void throwSettingNotFound(std::string_view name);
3838
[[noreturn]] static void throwValuelessSettingIsNotBool(std::string_view name, std::string_view type);
39+
[[noreturn]] static void throwValuelessSettingIsNotBool(std::string_view name);
3940
static void warningSettingNotFound(std::string_view name);
4041
static void flushWarnings();
4142

‎src/Interpreters/InterpreterSetQuery.cpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,12 @@ void InterpreterSetQuery::applySettingsFromQuery(const ASTPtr & ast, ContextMuta
179179
String & name = it->name;
180180
if ((!features.supports_settings || !features.has_builtin_setting_fn(name)) && context_settings.has(name))
181181
{
182+
/// A value-less `SETTINGS name` in a `CREATE` reaches the context here
183+
/// rather than through `executeForCurrentContext`, and the constraint check
184+
/// below converts the value first, so this has to come before it.
185+
context_settings.checkShorthandChange(*it);
182186
context_->checkSettingsConstraints(*it, SettingSource::QUERY);
183-
context_->setSetting(name, it->value);
187+
context_->applySettingChange(*it);
184188
it = engine_settings->changes.erase(it);
185189
}
186190
else

‎src/Storages/ObjectStorageQueue/registerQueueStorage.cpp‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "config.h"
22

33
#include <Core/FormatFactorySettings.h>
4+
#include <Core/BaseSettings.h>
45
#include <Core/Settings.h>
56
#include <Core/UUID.h>
67
#include <Common/Macros.h>
@@ -63,7 +64,7 @@ StoragePtr createQueueStorage(const StorageFactory::Arguments & args)
6364

6465
if (!is_attach && args.storage_def->settings)
6566
{
66-
if (auto * path_setting = args.storage_def->settings->changes.tryGet("keeper_path"))
67+
if (auto * path_setting = args.storage_def->settings->changes.tryGetChange("keeper_path"))
6768
{
6869
auto database = DatabaseCatalog::instance().tryGetDatabase(args.table_id.database_name);
6970
const String database_engine = database ? database->getEngineName() : "";
@@ -76,7 +77,13 @@ StoragePtr createQueueStorage(const StorageFactory::Arguments & args)
7677
/// and if UUID was explicitly passed in CREATE TABLE (like for ATTACH)
7778
bool allow_uuid_macro = is_on_cluster || is_replicated_database || args.query.attach || args.query.has_uuid;
7879

79-
String path = path_setting->safeGet<String>();
80+
/// This value is read before the settings are applied to `ObjectStorageQueueSettings`, so the
81+
/// value-less form `SETTINGS keeper_path` has not been rejected yet, and `safeGet` would report
82+
/// it as a `Bool` where a `String` was requested.
83+
if (path_setting->shorthand)
84+
BaseSettingsHelpers::throwValuelessSettingIsNotBool(path_setting->name);
85+
86+
String path = path_setting->value.safeGet<String>();
8087

8188
Macros::MacroExpansionInfo info;
8289
info.table_id = args.table_id;

‎src/Storages/StorageJoin.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <Common/quoteString.h>
1818
#include <Common/Exception.h>
1919
#include <Core/ColumnsWithTypeAndName.h>
20+
#include <Core/BaseSettings.h>
2021
#include <Core/Settings.h>
2122
#include <Interpreters/JoinUtils.h>
2223
#include <Formats/NativeWriter.h>
@@ -412,6 +413,14 @@ void registerStorageJoin(StorageFactory & factory)
412413
{
413414
for (const auto & setting : args.storage_def->settings->changes)
414415
{
416+
/// These settings are read here rather than applied to a `BaseSettings`, so there is no
417+
/// settings schema to check the value-less form `SETTINGS name` against - it stands for
418+
/// `name = true` and is only meaningful for the Bool ones. Without this check
419+
/// `SETTINGS max_rows_in_join` would silently become `max_rows_in_join = 1`.
420+
if (setting.shorthand && setting.name != "join_use_nulls" && setting.name != "join_any_take_last_row"
421+
&& setting.name != "any_join_distinct_right_table_keys" && setting.name != "persistent")
422+
BaseSettingsHelpers::throwValuelessSettingIsNotBool(setting.name);
423+
415424
if (setting.name == "join_use_nulls")
416425
join_use_nulls = setting.value;
417426
else if (setting.name == "max_rows_in_join")

‎src/Storages/StorageLogSettings.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <Storages/StorageLogSettings.h>
2+
#include <Core/BaseSettings.h>
23
#include <Disks/StoragePolicy.h>
34
#include <Interpreters/Context.h>
45
#include <Parsers/ASTCreateQuery.h>
@@ -27,6 +28,12 @@ String getDiskName(ASTStorage & storage_def, ContextPtr context)
2728
throw Exception(
2829
ErrorCodes::INVALID_SETTING_VALUE, "Could not specify `disk` and `storage_policy` at the same time for storage Log Family");
2930

31+
/// Both are Strings, and there is no settings schema for the Log family to reject the value-less
32+
/// form `SETTINGS disk` on its own, so `safeGet` would report a `Bool` where a `String` was wanted.
33+
for (const auto change : {disk_change, storage_policy_change})
34+
if (change != changes.end() && change->shorthand)
35+
BaseSettingsHelpers::throwValuelessSettingIsNotBool(change->name);
36+
3037
if (disk_change != changes.end())
3138
return disk_change->value.safeGet<String>();
3239

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SETTINGS ttl_only_drop_parts
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- A `SETTINGS` clause of a `CREATE` may mix engine settings and query settings, so
2+
-- `InterpreterSetQuery::applySettingsFromQuery` splits them and moves the query ones to the
3+
-- context on a path of its own. A value-less `name` there stands for `name = true` and must be
4+
-- rejected for a setting that is not Bool, on both halves of the split.
5+
6+
DROP TABLE IF EXISTS t_04663;
7+
8+
-- Query setting, moved to the context.
9+
CREATE TABLE t_04663 (a UInt8) ENGINE = Memory SETTINGS max_threads; -- { error TYPE_MISMATCH }
10+
11+
-- Enum-valued query setting: a type mismatch, not a `BAD_GET` from casting `true` to its type.
12+
CREATE TABLE t_04663 (a UInt8) ENGINE = Memory SETTINGS default_database_engine; -- { error TYPE_MISMATCH }
13+
14+
-- Engine setting, left in the storage definition.
15+
CREATE TABLE t_04663 (a UInt8) ENGINE = MergeTree ORDER BY a SETTINGS index_granularity; -- { error TYPE_MISMATCH }
16+
17+
-- A Bool engine setting is what the shorthand is for. The value-less form is kept by the formatter,
18+
-- so it is what `engine_full` shows.
19+
CREATE TABLE t_04663 (a UInt8) ENGINE = MergeTree ORDER BY a SETTINGS ttl_only_drop_parts;
20+
SELECT extract(engine_full, 'SETTINGS [^,]*') FROM system.tables WHERE database = currentDatabase() AND name = 't_04663';
21+
22+
DROP TABLE t_04663;

0 commit comments

Comments
 (0)