Skip to content

Commit

Permalink
moved date_time_overflow_mode to format settings
Browse files Browse the repository at this point in the history
  • Loading branch information
zvonand committed Oct 24, 2023
1 parent 79da0bd commit f37f7f3
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 143 deletions.
3 changes: 2 additions & 1 deletion src/Core/Settings.h
Expand Up @@ -812,7 +812,6 @@ class IColumn;
M(Bool, allow_create_index_without_type, false, "Allow CREATE INDEX query without TYPE. Query will be ignored. Made for SQL compatibility tests.", 0) \
M(Bool, create_index_ignore_unique, false, "Ignore UNIQUE keyword in CREATE UNIQUE INDEX. Made for SQL compatibility tests.", 0) \
M(Bool, print_pretty_type_names, false, "Print pretty type names in DESCRIBE query and toTypeName() function", 0) \
M(DateTimeOverflowMode, date_time_overflow_mode, DateTimeOverflowMode::Ignore, "Overflow mode for Date, Date32, DateTime, DateTime64 types. Possible values: 'ignore', 'throw', 'saturate'.", 0) \

// End of COMMON_SETTINGS
// Please add settings related to formats into the FORMAT_FACTORY_SETTINGS, move obsolete settings to OBSOLETE_SETTINGS and obsolete format settings to OBSOLETE_FORMAT_SETTINGS.
Expand Down Expand Up @@ -1076,6 +1075,8 @@ class IColumn;
\
M(Bool, dictionary_use_async_executor, false, "Execute a pipeline for reading from a dictionary with several threads. It's supported only by DIRECT dictionary with CLICKHOUSE source.", 0) \
M(Bool, precise_float_parsing, false, "Prefer more precise (but slower) float parsing algorithm", 0) \
M(DateTimeOverflowMode, date_time_overflow_mode, "ignore", "Overflow mode for Date, Date32, DateTime, DateTime64 types. Possible values: 'ignore', 'throw', 'saturate'.", 0) \


// End of FORMAT_FACTORY_SETTINGS
// Please add settings non-related to formats into the COMMON_SETTINGS above.
Expand Down
6 changes: 3 additions & 3 deletions src/Core/SettingsEnums.cpp
Expand Up @@ -191,8 +191,8 @@ IMPLEMENT_SETTING_ENUM(ExternalCommandStderrReaction, ErrorCodes::BAD_ARGUMENTS,
{"throw", ExternalCommandStderrReaction::THROW}})

IMPLEMENT_SETTING_ENUM(DateTimeOverflowMode, ErrorCodes::BAD_ARGUMENTS,
{{"throw", DateTimeOverflowMode::Throw},
{"ignore", DateTimeOverflowMode::Ignore},
{"saturate", DateTimeOverflowMode::Saturate}})
{{"throw", FormatSettings::DateTimeOverflowMode::Throw},
{"ignore", FormatSettings::DateTimeOverflowMode::Ignore},
{"saturate", FormatSettings::DateTimeOverflowMode::Saturate}})

}
9 changes: 1 addition & 8 deletions src/Core/SettingsEnums.h
Expand Up @@ -242,13 +242,6 @@ DECLARE_SETTING_ENUM(S3QueueAction)

DECLARE_SETTING_ENUM(ExternalCommandStderrReaction)

enum class DateTimeOverflowMode
{
Throw,
Ignore,
Saturate,
};

DECLARE_SETTING_ENUM(DateTimeOverflowMode)
DECLARE_SETTING_ENUM_WITH_RENAME(DateTimeOverflowMode, FormatSettings::DateTimeOverflowMode)

}
1 change: 1 addition & 0 deletions src/Formats/FormatFactory.cpp
Expand Up @@ -224,6 +224,7 @@ FormatSettings getFormatSettings(ContextPtr context, const Settings & settings)
format_settings.native.allow_types_conversion = settings.input_format_native_allow_types_conversion;
format_settings.max_parser_depth = context->getSettingsRef().max_parser_depth;
format_settings.client_protocol_version = context->getClientProtocolVersion();
format_settings.date_time_overflow_mode = settings.date_time_overflow_mode;

/// Validate avro_schema_registry_url with RemoteHostFilter when non-empty and in Server context
if (format_settings.schema.is_server)
Expand Down
9 changes: 9 additions & 0 deletions src/Formats/FormatSettings.h
Expand Up @@ -88,6 +88,15 @@ struct FormatSettings
IntervalOutputFormat output_format = IntervalOutputFormat::Numeric;
} interval;

enum class DateTimeOverflowMode
{
Ignore,
Throw,
Saturate
};

DateTimeOverflowMode date_time_overflow_mode = DateTimeOverflowMode::Ignore;

bool input_format_ipv4_default_on_conversion_error = false;
bool input_format_ipv6_default_on_conversion_error = false;

Expand Down
15 changes: 8 additions & 7 deletions src/Functions/DateTimeTransforms.h
Expand Up @@ -23,7 +23,8 @@ namespace DB
static constexpr auto microsecond_multiplier = 1000000;
static constexpr auto millisecond_multiplier = 1000;

static constexpr DateTimeOverflowMode default_date_time_overflow_mode = DateTimeOverflowMode::Ignore;
using DT_OVERFLOW_MODE = FormatSettings::DateTimeOverflowMode;
static constexpr DT_OVERFLOW_MODE default_date_time_overflow_mode = DT_OVERFLOW_MODE::Ignore;

namespace ErrorCodes
{
Expand Down Expand Up @@ -66,7 +67,7 @@ struct ZeroTransform
static UInt16 execute(UInt16, const DateLUTImpl &) { return 0; }
};

template <DateTimeOverflowMode date_time_overflow_mode = default_date_time_overflow_mode>
template <DT_OVERFLOW_MODE date_time_overflow_mode = default_date_time_overflow_mode>
struct ToDateImpl
{
static constexpr auto name = "toDate";
Expand All @@ -78,14 +79,14 @@ struct ToDateImpl

static UInt16 execute(Int64 t, const DateLUTImpl & time_zone)
{
if constexpr (date_time_overflow_mode == DateTimeOverflowMode::Saturate)
if constexpr (date_time_overflow_mode == DT_OVERFLOW_MODE::Saturate)
{
if (t < 0)
t = 0;
else if (t > MAX_DATE_TIMESTAMP)
t = MAX_DATE_TIMESTAMP;
}
else if constexpr (date_time_overflow_mode == DateTimeOverflowMode::Throw)
else if constexpr (date_time_overflow_mode == DT_OVERFLOW_MODE::Throw)
{
if (t < 0 || t > MAX_DATE_TIMESTAMP)
throw Exception(ErrorCodes::VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE, "Value {} is out of bounds of type Date", t);
Expand All @@ -98,14 +99,14 @@ struct ToDateImpl
}
static UInt16 execute(Int32 t, const DateLUTImpl &)
{
if constexpr (date_time_overflow_mode == DateTimeOverflowMode::Saturate)
if constexpr (date_time_overflow_mode == DT_OVERFLOW_MODE::Saturate)
{
if (t < 0)
return UInt16(0);
else if (t > DATE_LUT_MAX_DAY_NUM)
return UInt16(DATE_LUT_MAX_DAY_NUM);
}
else if constexpr (date_time_overflow_mode == DateTimeOverflowMode::Throw)
else if constexpr (date_time_overflow_mode == DT_OVERFLOW_MODE::Throw)
{
if (t < 0 || t > DATE_LUT_MAX_DAY_NUM)
throw Exception(ErrorCodes::VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE, "Value {} is out of bounds of type Date", t);
Expand All @@ -124,7 +125,7 @@ struct ToDateImpl
using FactorTransform = ZeroTransform;
};

template <DateTimeOverflowMode date_time_overflow_mode = default_date_time_overflow_mode>
template <DT_OVERFLOW_MODE date_time_overflow_mode = default_date_time_overflow_mode>
struct ToDate32Impl
{
static constexpr auto name = "toDate32";
Expand Down

0 comments on commit f37f7f3

Please sign in to comment.