From f2f47fc24db69bdff5b498c363d354c72fb6adb3 Mon Sep 17 00:00:00 2001 From: Shane Andrade Date: Mon, 17 Apr 2023 14:22:27 +0000 Subject: [PATCH 1/5] date_trunc function to always return DateTime type --- src/Functions/date_trunc.cpp | 248 ++++++++++++++++++++--------------- 1 file changed, 140 insertions(+), 108 deletions(-) diff --git a/src/Functions/date_trunc.cpp b/src/Functions/date_trunc.cpp index 016b8f4da5e..e2f22fab757 100644 --- a/src/Functions/date_trunc.cpp +++ b/src/Functions/date_trunc.cpp @@ -1,6 +1,6 @@ #include -#include #include +#include #include #include #include @@ -20,131 +20,163 @@ namespace ErrorCodes namespace { -class FunctionDateTrunc : public IFunction -{ -public: - static constexpr auto name = "dateTrunc"; - - explicit FunctionDateTrunc(ContextPtr context_) : context(context_) {} - - static FunctionPtr create(ContextPtr context) { return std::make_shared(context); } - - String getName() const override { return name; } - - bool isVariadic() const override { return true; } - bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; } - size_t getNumberOfArguments() const override { return 0; } - - DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override + class FunctionDateTrunc : public IFunction { - /// The first argument is a constant string with the name of datepart. + public: + static constexpr auto name = "dateTrunc"; - auto result_type_is_date = false; - String datepart_param; - auto check_first_argument = [&] { - const ColumnConst * datepart_column = checkAndGetColumnConst(arguments[0].column.get()); - if (!datepart_column) - throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "First argument for function {} must be constant string: " - "name of datepart", getName()); + explicit FunctionDateTrunc(ContextPtr context_) : context(context_) { } - datepart_param = datepart_column->getValue(); - if (datepart_param.empty()) - throw Exception(ErrorCodes::BAD_ARGUMENTS, "First argument (name of datepart) for function {} cannot be empty", - getName()); + static FunctionPtr create(ContextPtr context) { return std::make_shared(context); } - if (!IntervalKind::tryParseString(datepart_param, datepart_kind)) - throw Exception(ErrorCodes::BAD_ARGUMENTS, "{} doesn't look like datepart name in {}", datepart_param, getName()); + String getName() const override { return name; } - result_type_is_date = (datepart_kind == IntervalKind::Year) - || (datepart_kind == IntervalKind::Quarter) || (datepart_kind == IntervalKind::Month) - || (datepart_kind == IntervalKind::Week); - }; + bool isVariadic() const override { return true; } + bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; } + size_t getNumberOfArguments() const override { return 0; } - bool second_argument_is_date = false; - auto check_second_argument = [&] { - if (!isDate(arguments[1].type) && !isDateTime(arguments[1].type) && !isDateTime64(arguments[1].type)) - throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of 2nd argument of function {}. " - "Should be a date or a date with time", arguments[1].type->getName(), getName()); - - second_argument_is_date = isDate(arguments[1].type); - - if (second_argument_is_date && ((datepart_kind == IntervalKind::Hour) - || (datepart_kind == IntervalKind::Minute) || (datepart_kind == IntervalKind::Second))) - throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type Date of argument for function {}", getName()); - }; - - auto check_timezone_argument = [&] { - if (!WhichDataType(arguments[2].type).isString()) - throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument of function {}. " - "This argument is optional and must be a constant string with timezone name", - arguments[2].type->getName(), getName()); - - if (second_argument_is_date && result_type_is_date) - throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, - "The timezone argument of function {} with datepart '{}' " - "is allowed only when the 2nd argument has the type DateTime", - getName(), datepart_param); - }; - - if (arguments.size() == 2) + DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override { - check_first_argument(); - check_second_argument(); - } - else if (arguments.size() == 3) - { - check_first_argument(); - check_second_argument(); - check_timezone_argument(); - } - else - { - throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, - "Number of arguments for function {} doesn't match: passed {}, should be 2 or 3", - getName(), arguments.size()); + /// The first argument is a constant string with the name of datepart. + + result_type_is_date = false; + String datepart_param; + auto check_first_argument = [&] + { + const ColumnConst * datepart_column = checkAndGetColumnConst(arguments[0].column.get()); + if (!datepart_column) + throw Exception( + ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, + "First argument for function {} must be constant string: " + "name of datepart", + getName()); + + datepart_param = datepart_column->getValue(); + if (datepart_param.empty()) + throw Exception( + ErrorCodes::BAD_ARGUMENTS, "First argument (name of datepart) for function {} cannot be empty", getName()); + + if (!IntervalKind::tryParseString(datepart_param, datepart_kind)) + throw Exception(ErrorCodes::BAD_ARGUMENTS, "{} doesn't look like datepart name in {}", datepart_param, getName()); + + result_type_is_date = (datepart_kind == IntervalKind::Year) || (datepart_kind == IntervalKind::Quarter) + || (datepart_kind == IntervalKind::Month) || (datepart_kind == IntervalKind::Week); + }; + + bool second_argument_is_date = false; + auto check_second_argument = [&] + { + if (!isDate(arguments[1].type) && !isDateTime(arguments[1].type) && !isDateTime64(arguments[1].type)) + throw Exception( + ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, + "Illegal type {} of 2nd argument of function {}. " + "Should be a date or a date with time", + arguments[1].type->getName(), + getName()); + + second_argument_is_date = isDate(arguments[1].type); + + if (second_argument_is_date + && ((datepart_kind == IntervalKind::Hour) || (datepart_kind == IntervalKind::Minute) + || (datepart_kind == IntervalKind::Second))) + throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type Date of argument for function {}", getName()); + }; + + auto check_timezone_argument = [&] + { + if (!WhichDataType(arguments[2].type).isString()) + throw Exception( + ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, + "Illegal type {} of argument of function {}. " + "This argument is optional and must be a constant string with timezone name", + arguments[2].type->getName(), + getName()); + + if (second_argument_is_date && result_type_is_date) + throw Exception( + ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, + "The timezone argument of function {} with datepart '{}' " + "is allowed only when the 2nd argument has the type DateTime", + getName(), + datepart_param); + }; + + if (arguments.size() == 2) + { + check_first_argument(); + check_second_argument(); + } + else if (arguments.size() == 3) + { + check_first_argument(); + check_second_argument(); + check_timezone_argument(); + } + else + { + throw Exception( + ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, + "Number of arguments for function {} doesn't match: passed {}, should be 2 or 3", + getName(), + arguments.size()); + } + + if (result_type_is_date) + return std::make_shared(); + else + return std::make_shared(extractTimeZoneNameFromFunctionArguments(arguments, 2, 1)); } - if (result_type_is_date) - return std::make_shared(); - else - return std::make_shared(extractTimeZoneNameFromFunctionArguments(arguments, 2, 1)); - } + bool useDefaultImplementationForConstants() const override { return true; } + ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {0, 2}; } - bool useDefaultImplementationForConstants() const override { return true; } - ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {0, 2}; } + ColumnPtr + executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override + { + ColumnsWithTypeAndName temp_columns(arguments.size()); + temp_columns[0] = arguments[1]; - ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override - { - ColumnsWithTypeAndName temp_columns(arguments.size()); - temp_columns[0] = arguments[1]; + const UInt16 interval_value = 1; + const ColumnPtr interval_column = ColumnConst::create(ColumnInt64::create(1, interval_value), input_rows_count); + temp_columns[1] = {interval_column, std::make_shared(datepart_kind), ""}; - const UInt16 interval_value = 1; - const ColumnPtr interval_column = ColumnConst::create(ColumnInt64::create(1, interval_value), input_rows_count); - temp_columns[1] = {interval_column, std::make_shared(datepart_kind), ""}; + auto to_start_of_interval = FunctionFactory::instance().get("toStartOfInterval", context); - auto to_start_of_interval = FunctionFactory::instance().get("toStartOfInterval", context); + ColumnPtr truncated_column; + auto date_type = std::make_shared(); - if (arguments.size() == 2) - return to_start_of_interval->build(temp_columns)->execute(temp_columns, result_type, input_rows_count); + if (arguments.size() == 2) + truncated_column = to_start_of_interval->build(temp_columns) + ->execute(temp_columns, result_type_is_date ? date_type : result_type, input_rows_count); + else + { + temp_columns[2] = arguments[2]; + truncated_column = to_start_of_interval->build(temp_columns) + ->execute(temp_columns, result_type_is_date ? date_type : result_type, input_rows_count); + } - temp_columns[2] = arguments[2]; - return to_start_of_interval->build(temp_columns)->execute(temp_columns, result_type, input_rows_count); - } + if (!result_type_is_date) + return truncated_column; - bool hasInformationAboutMonotonicity() const override - { - return true; - } + ColumnsWithTypeAndName temp_truncated_column(1); + temp_truncated_column[0] = {truncated_column, date_type, ""}; - Monotonicity getMonotonicityForRange(const IDataType &, const Field &, const Field &) const override - { - return { .is_monotonic = true, .is_always_monotonic = true }; - } + auto to_date_time_or_default = FunctionFactory::instance().get("toDateTime", context); + return to_date_time_or_default->build(temp_truncated_column)->execute(temp_truncated_column, result_type, input_rows_count); + } -private: - ContextPtr context; - mutable IntervalKind::Kind datepart_kind = IntervalKind::Kind::Second; -}; + bool hasInformationAboutMonotonicity() const override { return true; } + + Monotonicity getMonotonicityForRange(const IDataType &, const Field &, const Field &) const override + { + return {.is_monotonic = true, .is_always_monotonic = true}; + } + + private: + ContextPtr context; + mutable IntervalKind::Kind datepart_kind = IntervalKind::Kind::Second; + mutable bool result_type_is_date = false; + }; } From 0521f58d6f625f24c6249a674113d1db0f2ce319 Mon Sep 17 00:00:00 2001 From: Shane Andrade Date: Mon, 17 Apr 2023 15:06:07 +0000 Subject: [PATCH 2/5] undo automatic indentation --- src/Functions/date_trunc.cpp | 292 +++++++++++++++++------------------ 1 file changed, 146 insertions(+), 146 deletions(-) diff --git a/src/Functions/date_trunc.cpp b/src/Functions/date_trunc.cpp index e2f22fab757..4cbc605f088 100644 --- a/src/Functions/date_trunc.cpp +++ b/src/Functions/date_trunc.cpp @@ -20,163 +20,163 @@ namespace ErrorCodes namespace { - class FunctionDateTrunc : public IFunction +class FunctionDateTrunc : public IFunction +{ +public: + static constexpr auto name = "dateTrunc"; + + explicit FunctionDateTrunc(ContextPtr context_) : context(context_) { } + + static FunctionPtr create(ContextPtr context) { return std::make_shared(context); } + + String getName() const override { return name; } + + bool isVariadic() const override { return true; } + bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; } + size_t getNumberOfArguments() const override { return 0; } + + DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override { - public: - static constexpr auto name = "dateTrunc"; + /// The first argument is a constant string with the name of datepart. - explicit FunctionDateTrunc(ContextPtr context_) : context(context_) { } - - static FunctionPtr create(ContextPtr context) { return std::make_shared(context); } - - String getName() const override { return name; } - - bool isVariadic() const override { return true; } - bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; } - size_t getNumberOfArguments() const override { return 0; } - - DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override + result_type_is_date = false; + String datepart_param; + auto check_first_argument = [&] { - /// The first argument is a constant string with the name of datepart. - - result_type_is_date = false; - String datepart_param; - auto check_first_argument = [&] - { - const ColumnConst * datepart_column = checkAndGetColumnConst(arguments[0].column.get()); - if (!datepart_column) - throw Exception( - ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, - "First argument for function {} must be constant string: " - "name of datepart", - getName()); - - datepart_param = datepart_column->getValue(); - if (datepart_param.empty()) - throw Exception( - ErrorCodes::BAD_ARGUMENTS, "First argument (name of datepart) for function {} cannot be empty", getName()); - - if (!IntervalKind::tryParseString(datepart_param, datepart_kind)) - throw Exception(ErrorCodes::BAD_ARGUMENTS, "{} doesn't look like datepart name in {}", datepart_param, getName()); - - result_type_is_date = (datepart_kind == IntervalKind::Year) || (datepart_kind == IntervalKind::Quarter) - || (datepart_kind == IntervalKind::Month) || (datepart_kind == IntervalKind::Week); - }; - - bool second_argument_is_date = false; - auto check_second_argument = [&] - { - if (!isDate(arguments[1].type) && !isDateTime(arguments[1].type) && !isDateTime64(arguments[1].type)) - throw Exception( - ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, - "Illegal type {} of 2nd argument of function {}. " - "Should be a date or a date with time", - arguments[1].type->getName(), - getName()); - - second_argument_is_date = isDate(arguments[1].type); - - if (second_argument_is_date - && ((datepart_kind == IntervalKind::Hour) || (datepart_kind == IntervalKind::Minute) - || (datepart_kind == IntervalKind::Second))) - throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type Date of argument for function {}", getName()); - }; - - auto check_timezone_argument = [&] - { - if (!WhichDataType(arguments[2].type).isString()) - throw Exception( - ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, - "Illegal type {} of argument of function {}. " - "This argument is optional and must be a constant string with timezone name", - arguments[2].type->getName(), - getName()); - - if (second_argument_is_date && result_type_is_date) - throw Exception( - ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, - "The timezone argument of function {} with datepart '{}' " - "is allowed only when the 2nd argument has the type DateTime", - getName(), - datepart_param); - }; - - if (arguments.size() == 2) - { - check_first_argument(); - check_second_argument(); - } - else if (arguments.size() == 3) - { - check_first_argument(); - check_second_argument(); - check_timezone_argument(); - } - else - { + const ColumnConst * datepart_column = checkAndGetColumnConst(arguments[0].column.get()); + if (!datepart_column) throw Exception( - ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, - "Number of arguments for function {} doesn't match: passed {}, should be 2 or 3", + ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, + "First argument for function {} must be constant string: " + "name of datepart", + getName()); + + datepart_param = datepart_column->getValue(); + if (datepart_param.empty()) + throw Exception( + ErrorCodes::BAD_ARGUMENTS, "First argument (name of datepart) for function {} cannot be empty", getName()); + + if (!IntervalKind::tryParseString(datepart_param, datepart_kind)) + throw Exception(ErrorCodes::BAD_ARGUMENTS, "{} doesn't look like datepart name in {}", datepart_param, getName()); + + result_type_is_date = (datepart_kind == IntervalKind::Year) || (datepart_kind == IntervalKind::Quarter) + || (datepart_kind == IntervalKind::Month) || (datepart_kind == IntervalKind::Week); + }; + + bool second_argument_is_date = false; + auto check_second_argument = [&] + { + if (!isDate(arguments[1].type) && !isDateTime(arguments[1].type) && !isDateTime64(arguments[1].type)) + throw Exception( + ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, + "Illegal type {} of 2nd argument of function {}. " + "Should be a date or a date with time", + arguments[1].type->getName(), + getName()); + + second_argument_is_date = isDate(arguments[1].type); + + if (second_argument_is_date + && ((datepart_kind == IntervalKind::Hour) || (datepart_kind == IntervalKind::Minute) + || (datepart_kind == IntervalKind::Second))) + throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type Date of argument for function {}", getName()); + }; + + auto check_timezone_argument = [&] + { + if (!WhichDataType(arguments[2].type).isString()) + throw Exception( + ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, + "Illegal type {} of argument of function {}. " + "This argument is optional and must be a constant string with timezone name", + arguments[2].type->getName(), + getName()); + + if (second_argument_is_date && result_type_is_date) + throw Exception( + ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, + "The timezone argument of function {} with datepart '{}' " + "is allowed only when the 2nd argument has the type DateTime", getName(), - arguments.size()); - } + datepart_param); + }; - if (result_type_is_date) - return std::make_shared(); - else - return std::make_shared(extractTimeZoneNameFromFunctionArguments(arguments, 2, 1)); - } - - bool useDefaultImplementationForConstants() const override { return true; } - ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {0, 2}; } - - ColumnPtr - executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override + if (arguments.size() == 2) { - ColumnsWithTypeAndName temp_columns(arguments.size()); - temp_columns[0] = arguments[1]; - - const UInt16 interval_value = 1; - const ColumnPtr interval_column = ColumnConst::create(ColumnInt64::create(1, interval_value), input_rows_count); - temp_columns[1] = {interval_column, std::make_shared(datepart_kind), ""}; - - auto to_start_of_interval = FunctionFactory::instance().get("toStartOfInterval", context); - - ColumnPtr truncated_column; - auto date_type = std::make_shared(); - - if (arguments.size() == 2) - truncated_column = to_start_of_interval->build(temp_columns) - ->execute(temp_columns, result_type_is_date ? date_type : result_type, input_rows_count); - else - { - temp_columns[2] = arguments[2]; - truncated_column = to_start_of_interval->build(temp_columns) - ->execute(temp_columns, result_type_is_date ? date_type : result_type, input_rows_count); - } - - if (!result_type_is_date) - return truncated_column; - - ColumnsWithTypeAndName temp_truncated_column(1); - temp_truncated_column[0] = {truncated_column, date_type, ""}; - - auto to_date_time_or_default = FunctionFactory::instance().get("toDateTime", context); - return to_date_time_or_default->build(temp_truncated_column)->execute(temp_truncated_column, result_type, input_rows_count); + check_first_argument(); + check_second_argument(); } - - bool hasInformationAboutMonotonicity() const override { return true; } - - Monotonicity getMonotonicityForRange(const IDataType &, const Field &, const Field &) const override + else if (arguments.size() == 3) { - return {.is_monotonic = true, .is_always_monotonic = true}; + check_first_argument(); + check_second_argument(); + check_timezone_argument(); + } + else + { + throw Exception( + ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, + "Number of arguments for function {} doesn't match: passed {}, should be 2 or 3", + getName(), + arguments.size()); } - private: - ContextPtr context; - mutable IntervalKind::Kind datepart_kind = IntervalKind::Kind::Second; - mutable bool result_type_is_date = false; - }; + if (result_type_is_date) + return std::make_shared(); + else + return std::make_shared(extractTimeZoneNameFromFunctionArguments(arguments, 2, 1)); + } + + bool useDefaultImplementationForConstants() const override { return true; } + ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {0, 2}; } + + ColumnPtr + executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override + { + ColumnsWithTypeAndName temp_columns(arguments.size()); + temp_columns[0] = arguments[1]; + + const UInt16 interval_value = 1; + const ColumnPtr interval_column = ColumnConst::create(ColumnInt64::create(1, interval_value), input_rows_count); + temp_columns[1] = {interval_column, std::make_shared(datepart_kind), ""}; + + auto to_start_of_interval = FunctionFactory::instance().get("toStartOfInterval", context); + + ColumnPtr truncated_column; + auto date_type = std::make_shared(); + + if (arguments.size() == 2) + truncated_column = to_start_of_interval->build(temp_columns) + ->execute(temp_columns, result_type_is_date ? date_type : result_type, input_rows_count); + else + { + temp_columns[2] = arguments[2]; + truncated_column = to_start_of_interval->build(temp_columns) + ->execute(temp_columns, result_type_is_date ? date_type : result_type, input_rows_count); + } + + if (!result_type_is_date) + return truncated_column; + + ColumnsWithTypeAndName temp_truncated_column(1); + temp_truncated_column[0] = {truncated_column, date_type, ""}; + + auto to_date_time_or_default = FunctionFactory::instance().get("toDateTime", context); + return to_date_time_or_default->build(temp_truncated_column)->execute(temp_truncated_column, result_type, input_rows_count); + } + + bool hasInformationAboutMonotonicity() const override { return true; } + + Monotonicity getMonotonicityForRange(const IDataType &, const Field &, const Field &) const override + { + return {.is_monotonic = true, .is_always_monotonic = true}; + } + +private: + ContextPtr context; + mutable IntervalKind::Kind datepart_kind = IntervalKind::Kind::Second; + mutable bool result_type_is_date = false; +}; } From 6867101d8d44f9f6483f12d2b13dad984f7c90da Mon Sep 17 00:00:00 2001 From: Shane Andrade Date: Thu, 20 Apr 2023 00:04:00 -0700 Subject: [PATCH 3/5] Update src/Functions/date_trunc.cpp Co-authored-by: Nikolay Degterinsky <43110995+evillique@users.noreply.github.com> --- src/Functions/date_trunc.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Functions/date_trunc.cpp b/src/Functions/date_trunc.cpp index 4cbc605f088..26654b746d4 100644 --- a/src/Functions/date_trunc.cpp +++ b/src/Functions/date_trunc.cpp @@ -130,8 +130,7 @@ public: bool useDefaultImplementationForConstants() const override { return true; } ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {0, 2}; } - ColumnPtr - executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override + ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override { ColumnsWithTypeAndName temp_columns(arguments.size()); temp_columns[0] = arguments[1]; From 4b084c15d075e3622250e078e1e815d3ec7cb133 Mon Sep 17 00:00:00 2001 From: mauidude Date: Mon, 24 Apr 2023 21:28:07 +0000 Subject: [PATCH 4/5] update tests, address code review comments --- src/Functions/date_trunc.cpp | 25 +++------ .../00189_time_zones_long.reference | 56 +++++++++---------- ...21_datetime64_compatibility_long.reference | 8 +-- 3 files changed, 39 insertions(+), 50 deletions(-) diff --git a/src/Functions/date_trunc.cpp b/src/Functions/date_trunc.cpp index 26654b746d4..87fff0b7f3c 100644 --- a/src/Functions/date_trunc.cpp +++ b/src/Functions/date_trunc.cpp @@ -39,7 +39,7 @@ public: { /// The first argument is a constant string with the name of datepart. - result_type_is_date = false; + intermediate_type_is_date = false; String datepart_param; auto check_first_argument = [&] { @@ -59,7 +59,7 @@ public: if (!IntervalKind::tryParseString(datepart_param, datepart_kind)) throw Exception(ErrorCodes::BAD_ARGUMENTS, "{} doesn't look like datepart name in {}", datepart_param, getName()); - result_type_is_date = (datepart_kind == IntervalKind::Year) || (datepart_kind == IntervalKind::Quarter) + intermediate_type_is_date = (datepart_kind == IntervalKind::Year) || (datepart_kind == IntervalKind::Quarter) || (datepart_kind == IntervalKind::Month) || (datepart_kind == IntervalKind::Week); }; @@ -91,14 +91,6 @@ public: "This argument is optional and must be a constant string with timezone name", arguments[2].type->getName(), getName()); - - if (second_argument_is_date && result_type_is_date) - throw Exception( - ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, - "The timezone argument of function {} with datepart '{}' " - "is allowed only when the 2nd argument has the type DateTime", - getName(), - datepart_param); }; if (arguments.size() == 2) @@ -121,10 +113,7 @@ public: arguments.size()); } - if (result_type_is_date) - return std::make_shared(); - else - return std::make_shared(extractTimeZoneNameFromFunctionArguments(arguments, 2, 1)); + return std::make_shared(extractTimeZoneNameFromFunctionArguments(arguments, 2, 1)); } bool useDefaultImplementationForConstants() const override { return true; } @@ -146,15 +135,15 @@ public: if (arguments.size() == 2) truncated_column = to_start_of_interval->build(temp_columns) - ->execute(temp_columns, result_type_is_date ? date_type : result_type, input_rows_count); + ->execute(temp_columns, intermediate_type_is_date ? date_type : result_type, input_rows_count); else { temp_columns[2] = arguments[2]; truncated_column = to_start_of_interval->build(temp_columns) - ->execute(temp_columns, result_type_is_date ? date_type : result_type, input_rows_count); + ->execute(temp_columns, intermediate_type_is_date ? date_type : result_type, input_rows_count); } - if (!result_type_is_date) + if (!intermediate_type_is_date) return truncated_column; ColumnsWithTypeAndName temp_truncated_column(1); @@ -174,7 +163,7 @@ public: private: ContextPtr context; mutable IntervalKind::Kind datepart_kind = IntervalKind::Kind::Second; - mutable bool result_type_is_date = false; + mutable bool intermediate_type_is_date = false; }; } diff --git a/tests/queries/0_stateless/00189_time_zones_long.reference b/tests/queries/0_stateless/00189_time_zones_long.reference index 8717a662771..d41c925bbe5 100644 --- a/tests/queries/0_stateless/00189_time_zones_long.reference +++ b/tests/queries/0_stateless/00189_time_zones_long.reference @@ -246,18 +246,18 @@ toUnixTimestamp 1426415400 1426415400 date_trunc -2019-01-01 -2020-01-01 -2020-01-01 -2019-10-01 -2020-01-01 -2020-01-01 -2019-12-01 -2020-01-01 -2020-01-01 -2019-12-30 -2019-12-30 -2019-12-30 +2019-01-01 00:00:00 +2020-01-01 00:00:00 +2020-01-01 00:00:00 +2019-10-01 00:00:00 +2020-01-01 00:00:00 +2020-01-01 00:00:00 +2019-12-01 00:00:00 +2020-01-01 00:00:00 +2020-01-01 00:00:00 +2019-12-30 00:00:00 +2019-12-30 00:00:00 +2019-12-30 00:00:00 2019-12-31 00:00:00 2020-01-01 00:00:00 2020-01-02 00:00:00 @@ -270,18 +270,18 @@ date_trunc 2019-12-31 20:11:22 2020-01-01 12:11:22 2020-01-02 05:11:22 -2019-01-01 -2020-01-01 -2020-01-01 -2019-10-01 -2020-01-01 -2020-01-01 -2019-12-01 -2020-01-01 -2020-01-01 -2019-12-30 -2019-12-30 -2019-12-30 +2019-01-01 00:00:00 +2020-01-01 00:00:00 +2020-01-01 00:00:00 +2019-10-01 00:00:00 +2020-01-01 00:00:00 +2020-01-01 00:00:00 +2019-12-01 00:00:00 +2020-01-01 00:00:00 +2020-01-01 00:00:00 +2019-12-30 00:00:00 +2019-12-30 00:00:00 +2019-12-30 00:00:00 2019-12-31 00:00:00 2020-01-01 00:00:00 2020-01-02 00:00:00 @@ -294,8 +294,8 @@ date_trunc 2019-12-31 20:11:22 2020-01-01 12:11:22 2020-01-02 05:11:22 -2020-01-01 -2020-01-01 -2020-01-01 -2019-12-30 +2020-01-01 00:00:00 +2020-01-01 00:00:00 +2020-01-01 00:00:00 +2019-12-30 00:00:00 2020-01-01 00:00:00 diff --git a/tests/queries/0_stateless/00921_datetime64_compatibility_long.reference b/tests/queries/0_stateless/00921_datetime64_compatibility_long.reference index 62de3a149a7..0c6bedb347a 100644 --- a/tests/queries/0_stateless/00921_datetime64_compatibility_long.reference +++ b/tests/queries/0_stateless/00921_datetime64_compatibility_long.reference @@ -135,13 +135,13 @@ Code: 43 ------------------------------------------ SELECT date_trunc(\'year\', N, \'Asia/Istanbul\') Code: 43 -"Date","2019-01-01" -"Date","2019-01-01" +"DateTime","2019-01-01 00:00:00" +"DateTime","2019-01-01 00:00:00" ------------------------------------------ SELECT date_trunc(\'month\', N, \'Asia/Istanbul\') Code: 43 -"Date","2019-09-01" -"Date","2019-09-01" +"DateTime","2019-09-01 00:00:00" +"DateTime","2019-09-01 00:00:00" ------------------------------------------ SELECT date_trunc(\'day\', N, \'Asia/Istanbul\') "DateTime('Asia/Istanbul')","2019-09-16 00:00:00" From bbc5577bcbf5c16af8c89fde210712a7313cabd3 Mon Sep 17 00:00:00 2001 From: Shane Andrade Date: Mon, 1 May 2023 08:59:55 -0700 Subject: [PATCH 5/5] fix failing tests --- .../00921_datetime64_compatibility_long.reference | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/queries/0_stateless/00921_datetime64_compatibility_long.reference b/tests/queries/0_stateless/00921_datetime64_compatibility_long.reference index 0c6bedb347a..4f964f2478f 100644 --- a/tests/queries/0_stateless/00921_datetime64_compatibility_long.reference +++ b/tests/queries/0_stateless/00921_datetime64_compatibility_long.reference @@ -135,13 +135,13 @@ Code: 43 ------------------------------------------ SELECT date_trunc(\'year\', N, \'Asia/Istanbul\') Code: 43 -"DateTime","2019-01-01 00:00:00" -"DateTime","2019-01-01 00:00:00" +"DateTime('Asia/Istanbul')","2019-01-01 00:00:00" +"DateTime('Asia/Istanbul')","2019-01-01 00:00:00" ------------------------------------------ SELECT date_trunc(\'month\', N, \'Asia/Istanbul\') Code: 43 -"DateTime","2019-09-01 00:00:00" -"DateTime","2019-09-01 00:00:00" +"DateTime('Asia/Istanbul')","2019-09-01 00:00:00" +"DateTime('Asia/Istanbul')","2019-09-01 00:00:00" ------------------------------------------ SELECT date_trunc(\'day\', N, \'Asia/Istanbul\') "DateTime('Asia/Istanbul')","2019-09-16 00:00:00"