PR review feedback incorporated

This commit is contained in:
shiyer7474 2024-09-26 03:35:06 +00:00
parent 96317da8d2
commit 454137f731
7 changed files with 69 additions and 36 deletions

View File

@ -2789,33 +2789,38 @@ Result:
- [Custom Settings](../../operations/settings/index.md#custom_settings) - [Custom Settings](../../operations/settings/index.md#custom_settings)
## getSettingOrNull ## getSettingOrDefault
Returns the current value of a [custom setting](../../operations/settings/index.md#custom_settings) or returns NULL if setting is undefined. Returns the current value of a [custom setting](../../operations/settings/index.md#custom_settings) or returns the default value specified in the 2nd argument if the custom setting is not set in the current profile.
**Syntax** **Syntax**
```sql ```sql
getSettingOrNull('custom_setting'); getSettingOrDefault('custom_setting', default_value);
``` ```
**Parameter** **Parameter**
- `custom_setting` — The setting name. [String](../data-types/string.md). - `custom_setting` — The setting name. [String](../data-types/string.md).
- `default_value` — Value to return if custom_setting is not set. Value may be of any data type or Null.
**Returned value** **Returned value**
- The setting's current value or NULL if setting is undefined. - The setting's current value or default_value if setting is not set.
**Example** **Example**
```sql ```sql
SELECT getSetting('custom_xyz'); SELECT getSettingOrDefault('custom_undef1', 'my_value');
SELECT getSettingOrDefault('custom_undef2', 100);
SELECT getSettingOrDefault('custom_undef3', NULL);
``` ```
Result: Result:
``` ```
my_value
100
NULL NULL
``` ```

View File

@ -19,22 +19,30 @@ namespace ErrorCodes
namespace namespace
{ {
enum class ErrorHandling : uint8_t enum class ErrorHandlingMode : uint8_t
{ {
Exception, Exception, /// Raise exception if setting not found (getSetting())
Default, Default, /// Return default value if setting not found (getSettingOrDefault())
}; };
struct NameGetSetting{ static constexpr auto name = "getSetting"; };
struct NameGetSettingOrDefault{ static constexpr auto name = "getSettingOrDefault"; };
/// Get the value of a setting. /// Get the value of a setting.
template <typename Name, ErrorHandlingMode mode>
class FunctionGetSetting : public IFunction, WithContext class FunctionGetSetting : public IFunction, WithContext
{ {
public: public:
explicit FunctionGetSetting(ContextPtr context_, String name_, ErrorHandling error_handling_) : WithContext(context_), name(name_), error_handling(error_handling_) {} static constexpr auto name = Name::name;
static FunctionPtr create(ContextPtr context_) { return std::make_shared<FunctionGetSetting>(context_); }
explicit FunctionGetSetting(ContextPtr context_) : WithContext(context_) {}
String getName() const override { return name; } String getName() const override { return name; }
bool isDeterministic() const override { return false; } bool isDeterministic() const override { return false; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; } bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
size_t getNumberOfArguments() const override { return ( name == "getSettingOrDefault" ? 2 : 1 ); } size_t getNumberOfArguments() const override { return ( mode == ErrorHandlingMode::Default ? 2 : 1 ); }
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {0,1}; } ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {0,1}; }
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
@ -61,7 +69,7 @@ private:
throw Exception(ErrorCodes::ILLEGAL_COLUMN, throw Exception(ErrorCodes::ILLEGAL_COLUMN,
"The argument of function {} should be a constant string with the name of a setting", "The argument of function {} should be a constant string with the name of a setting",
String{name}); String{name});
if (arguments.size() == 2) if (mode == ErrorHandlingMode::Default)
{ {
const auto * default_value_column = arguments[1].column.get(); const auto * default_value_column = arguments[1].column.get();
if (!default_value_column || default_value_column->size() != 1) if (!default_value_column || default_value_column->size() != 1)
@ -79,9 +87,9 @@ private:
} }
catch(...) catch(...)
{ {
if (error_handling == ErrorHandling::Exception) if (mode == ErrorHandlingMode::Exception)
throw; throw;
else if (error_handling == ErrorHandling::Default) else if (mode == ErrorHandlingMode::Default)
{ {
auto default_value_column = arguments[1].column; auto default_value_column = arguments[1].column;
@ -90,22 +98,21 @@ private:
} }
return setting_value; return setting_value;
} }
String name;
ErrorHandling error_handling;
}; };
} }
using FunctionGetSettingWithException = FunctionGetSetting<NameGetSetting, ErrorHandlingMode::Exception>;
using FunctionGetSettingWithDefault = FunctionGetSetting<NameGetSettingOrDefault, ErrorHandlingMode::Default>;
REGISTER_FUNCTION(GetSetting) REGISTER_FUNCTION(GetSetting)
{ {
factory.registerFunction("getSetting", factory.registerFunction<FunctionGetSettingWithException>();
[](ContextPtr context){ return std::make_shared<FunctionGetSetting>(context, "getSetting", ErrorHandling::Exception); });
} }
REGISTER_FUNCTION(GetSettingOrDefault) REGISTER_FUNCTION(GetSettingOrDefault)
{ {
factory.registerFunction("getSettingOrDefault", factory.registerFunction<FunctionGetSettingWithDefault>();
[](ContextPtr context){ return std::make_shared<FunctionGetSetting>(context, "getSettingOrDefault", ErrorHandling::Default); });
} }
} }

View File

@ -37,9 +37,3 @@ custom_null NULL
\N Nullable(Nothing) \N Nullable(Nothing)
custom_null NULL custom_null NULL
CREATE SETTINGS PROFILE `s2_01418` SETTINGS custom_null = NULL CREATE SETTINGS PROFILE `s2_01418` SETTINGS custom_null = NULL
my_default_value
test
\N
50
1
1

View File

@ -61,12 +61,3 @@ SELECT name, value FROM system.settings WHERE name = 'custom_null';
CREATE SETTINGS PROFILE s2_01418 SETTINGS custom_null = NULL; CREATE SETTINGS PROFILE s2_01418 SETTINGS custom_null = NULL;
SHOW CREATE SETTINGS PROFILE s2_01418; SHOW CREATE SETTINGS PROFILE s2_01418;
DROP SETTINGS PROFILE s2_01418; DROP SETTINGS PROFILE s2_01418;
SELECT getSetting('custom_xyz') as v; -- { serverError UNKNOWN_SETTING } -- Setting not found.
SELECT getSettingOrDefault('custom_xyz','my_default_value') as v;
SELECT getSettingOrDefault('custom_compound.identifier.v1','should not be seen') as v;
SELECT getSettingOrDefault('custom_xyz', NULL) as v;
SELECT getSettingOrDefault('custom_xyz', 50) as v;
SELECT getSettingOrDefault(1, 'def') as v; -- { serverError ILLEGAL_TYPE_OF_ARGUMENT }
SELECT count(*) FROM numbers(10) WHERE number = getSettingOrDefault('custom_role',5);
SELECT isNull(getSettingOrDefault('custom_xyz',NULL)) as v;

View File

@ -0,0 +1,10 @@
value_a
value_b
\N
5
default_e
500
\N
1
1
backup

View File

@ -0,0 +1,26 @@
SET custom_a = 'value_a';
SET custom_b = 'value_b';
SET custom_c = null;
SET custom_d = 5;
SELECT getSettingOrDefault('custom_a','default_a');
SELECT getSettingOrDefault('custom_b','default_b');
SELECT getSettingOrDefault('custom_c','default_c');
SELECT getSettingOrDefault('custom_d','default_d');
SELECT getSetting('custom_e'); -- { serverError UNKNOWN_SETTING }
SELECT getSettingOrDefault('custom_e','default_e');
SELECT getSettingOrDefault('custom_e',500);
SELECT getSettingOrDefault('custom_e',null);
SELECT isNull(getSettingOrDefault('custom_e',null));
SELECT getSettingOrDefault('custom_e'); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH }
SELECT getSettingOrDefault(115,'name should be string'); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT }
SELECT count(*) FROM numbers(10) WHERE number = getSettingOrDefault('custom_e',5);
SET custom_e_backup = 'backup';
SELECT getSettingOrDefault('custom_e', getSetting('custom_e_backup'));

View File

@ -1734,7 +1734,7 @@ getMacro
getOSKernelVersion getOSKernelVersion
getServerPort getServerPort
getSetting getSetting
getSettingOrNull getSettingOrDefault
getSizeOfEnumType getSizeOfEnumType
getSubcolumn getSubcolumn
getTypeSerializationStreams getTypeSerializationStreams