diff --git a/src/Functions/FunctionDateOrDateTimeToSomething.h b/src/Functions/FunctionDateOrDateTimeToSomething.h index 5269eecea37..d734c7f87c1 100644 --- a/src/Functions/FunctionDateOrDateTimeToSomething.h +++ b/src/Functions/FunctionDateOrDateTimeToSomething.h @@ -148,8 +148,10 @@ public: const IFunction::Monotonicity is_monotonic = { .is_monotonic = true }; const IFunction::Monotonicity is_not_monotonic; - /// This method is called only if the function has one argument. Therefore, we do not care about the non-local time zone. - const DateLUTImpl & date_lut = DateLUT::instance(); + const DateLUTImpl * date_lut = &DateLUT::instance(); + if (const auto * timezone = dynamic_cast(&type)) + date_lut = &timezone->getTimeZone(); + if (left.isNull() || right.isNull()) return is_not_monotonic; @@ -157,20 +159,20 @@ public: if (checkAndGetDataType(&type)) { - return Transform::FactorTransform::execute(UInt16(left.get()), date_lut) - == Transform::FactorTransform::execute(UInt16(right.get()), date_lut) + return Transform::FactorTransform::execute(UInt16(left.get()), *date_lut) + == Transform::FactorTransform::execute(UInt16(right.get()), *date_lut) ? is_monotonic : is_not_monotonic; } else if (checkAndGetDataType(&type)) { - return Transform::FactorTransform::execute(Int32(left.get()), date_lut) - == Transform::FactorTransform::execute(Int32(right.get()), date_lut) + return Transform::FactorTransform::execute(Int32(left.get()), *date_lut) + == Transform::FactorTransform::execute(Int32(right.get()), *date_lut) ? is_monotonic : is_not_monotonic; } else { - return Transform::FactorTransform::execute(UInt32(left.get()), date_lut) - == Transform::FactorTransform::execute(UInt32(right.get()), date_lut) + return Transform::FactorTransform::execute(UInt32(left.get()), *date_lut) + == Transform::FactorTransform::execute(UInt32(right.get()), *date_lut) ? is_monotonic : is_not_monotonic; } } diff --git a/tests/queries/0_stateless/02346_to_hour_monotonicity_fix.reference b/tests/queries/0_stateless/02346_to_hour_monotonicity_fix.reference new file mode 100644 index 00000000000..5d49984b71b --- /dev/null +++ b/tests/queries/0_stateless/02346_to_hour_monotonicity_fix.reference @@ -0,0 +1 @@ +5 8 42 diff --git a/tests/queries/0_stateless/02346_to_hour_monotonicity_fix.sql b/tests/queries/0_stateless/02346_to_hour_monotonicity_fix.sql new file mode 100644 index 00000000000..5eb396970d7 --- /dev/null +++ b/tests/queries/0_stateless/02346_to_hour_monotonicity_fix.sql @@ -0,0 +1,8 @@ +drop table if exists test_tz_hour; + +create table test_tz_hour(t DateTime, x String) engine MergeTree partition by toYYYYMMDD(t) order by x; +insert into test_tz_hour select toDateTime('2021-06-01 00:00:00', 'UTC') + number * 600, 'x' from numbers(1e3); + +select toHour(toTimeZone(t, 'UTC')) as toHour_UTC, toHour(toTimeZone(t, 'Asia/Jerusalem')) as toHour_Israel, count() from test_tz_hour where toHour_Israel = 8 group by toHour_UTC, toHour_Israel; + +drop table test_tz_hour;