Merge pull request #23392 from ClickHouse/fix-4854

Keep default timezone on DateTime operations if it was not provided explicitly #4854
This commit is contained in:
alexey-milovidov 2021-04-21 15:00:59 +03:00 committed by GitHub
commit 20a5fed53e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 3 deletions

View File

@ -19,9 +19,12 @@ public:
TimezoneMixin(const TimezoneMixin &) = default;
const DateLUTImpl & getTimeZone() const { return time_zone; }
bool hasExplicitTimeZone() const { return has_explicit_time_zone; }
protected:
/// true if time zone name was provided in data type parameters, false if it's using default time zone.
bool has_explicit_time_zone;
const DateLUTImpl & time_zone;
const DateLUTImpl & utc_time_zone;
};

View File

@ -5,7 +5,6 @@
#include <Functions/CustomWeekTransforms.h>
#include <Functions/IFunctionImpl.h>
#include <Functions/TransformDateTime64.h>
#include <Functions/extractTimeZoneFromFunctionArguments.h>
#include <IO/WriteHelpers.h>

View File

@ -44,9 +44,9 @@ std::string extractTimeZoneNameFromFunctionArguments(const ColumnsWithTypeAndNam
/// If time zone is attached to an argument of type DateTime.
if (const auto * type = checkAndGetDataType<DataTypeDateTime>(arguments[datetime_arg_num].type.get()))
return type->getTimeZone().getTimeZone();
return type->hasExplicitTimeZone() ? type->getTimeZone().getTimeZone() : std::string();
if (const auto * type = checkAndGetDataType<DataTypeDateTime64>(arguments[datetime_arg_num].type.get()))
return type->getTimeZone().getTimeZone();
return type->hasExplicitTimeZone() ? type->getTimeZone().getTimeZone() : std::string();
return {};
}

View File

@ -13,6 +13,7 @@ namespace DB
class Block;
/// Determine working timezone either from optional argument with time zone name or from time zone in DateTime type of argument.
/// Returns empty string if default time zone should be used.
std::string extractTimeZoneNameFromFunctionArguments(
const ColumnsWithTypeAndName & arguments, size_t time_zone_arg_num, size_t datetime_arg_num);

View File

@ -0,0 +1,6 @@
DateTime
DateTime
DateTime(\'UTC\')
DateTime64(3)
DateTime64(3)
DateTime64(3, \'UTC\')

View File

@ -0,0 +1,26 @@
SELECT toTypeName(now());
SELECT toTypeName(now() - 1);
SELECT toTypeName(now('UTC') - 1);
SELECT toTypeName(now64(3));
SELECT toTypeName(now64(3) - 1);
SELECT toTypeName(toTimeZone(now64(3), 'UTC') - 1);
DROP TABLE IF EXISTS tt_null;
DROP TABLE IF EXISTS tt;
DROP TABLE IF EXISTS tt_mv;
create table tt_null(p String) engine = Null;
create table tt(p String,tmin AggregateFunction(min, DateTime))
engine = AggregatingMergeTree order by p;
create materialized view tt_mv to tt as
select p, minState(now() - interval 30 minute) as tmin
from tt_null group by p;
insert into tt_null values('x');
DROP TABLE tt_null;
DROP TABLE tt;
DROP TABLE tt_mv;