2019-10-02 05:53:38 +00:00
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
2017-03-12 10:13:45 +00:00
|
|
|
|
2019-09-26 15:12:40 +00:00
|
|
|
#include <Columns/ColumnVector.h>
|
2019-10-02 05:53:38 +00:00
|
|
|
#include <Common/assert_cast.h>
|
|
|
|
#include <common/DateLUT.h>
|
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
2018-06-10 19:22:49 +00:00
|
|
|
#include <Formats/FormatSettings.h>
|
2017-11-05 05:32:22 +00:00
|
|
|
#include <IO/Operators.h>
|
2019-10-02 05:53:38 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteBufferFromString.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/parseDateTimeBestEffort.h>
|
2017-11-05 05:32:22 +00:00
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
|
2019-10-02 05:53:38 +00:00
|
|
|
namespace
|
2017-03-12 10:13:45 +00:00
|
|
|
{
|
2019-10-02 05:53:38 +00:00
|
|
|
using namespace DB;
|
2020-03-08 22:38:12 +00:00
|
|
|
inline void readText(time_t & x, ReadBuffer & istr, const FormatSettings & settings, const DateLUTImpl & time_zone, const DateLUTImpl & utc_time_zone)
|
2019-10-02 05:53:38 +00:00
|
|
|
{
|
|
|
|
switch (settings.date_time_input_format)
|
|
|
|
{
|
|
|
|
case FormatSettings::DateTimeInputFormat::Basic:
|
|
|
|
readDateTimeText(x, istr, time_zone);
|
|
|
|
return;
|
|
|
|
case FormatSettings::DateTimeInputFormat::BestEffort:
|
|
|
|
parseDateTimeBestEffort(x, istr, time_zone, utc_time_zone);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-02 22:40:45 +00:00
|
|
|
|
2019-10-02 05:53:38 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2019-03-28 22:33:01 +00:00
|
|
|
|
2019-10-14 08:38:03 +00:00
|
|
|
TimezoneMixin::TimezoneMixin(const String & time_zone_name)
|
2017-11-05 05:32:22 +00:00
|
|
|
: has_explicit_time_zone(!time_zone_name.empty()),
|
2018-06-08 02:56:37 +00:00
|
|
|
time_zone(DateLUT::instance(time_zone_name)),
|
|
|
|
utc_time_zone(DateLUT::instance("UTC"))
|
2019-10-02 05:53:38 +00:00
|
|
|
{}
|
2017-03-12 10:13:45 +00:00
|
|
|
|
2020-03-10 18:16:14 +00:00
|
|
|
DataTypeDateTime::DataTypeDateTime(const String & time_zone_name)
|
|
|
|
: TimezoneMixin(time_zone_name)
|
2019-03-28 22:33:01 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-10-21 14:05:52 +00:00
|
|
|
DataTypeDateTime::DataTypeDateTime(const TimezoneMixin & time_zone_)
|
|
|
|
: TimezoneMixin(time_zone_)
|
|
|
|
{}
|
|
|
|
|
2019-10-14 08:38:03 +00:00
|
|
|
String DataTypeDateTime::doGetName() const
|
2017-03-12 10:13:45 +00:00
|
|
|
{
|
2017-11-05 05:32:22 +00:00
|
|
|
if (!has_explicit_time_zone)
|
2020-03-10 18:16:14 +00:00
|
|
|
return "DateTime";
|
2017-11-05 05:32:22 +00:00
|
|
|
|
|
|
|
WriteBufferFromOwnString out;
|
2020-03-10 18:16:14 +00:00
|
|
|
out << "DateTime(" << quote << time_zone.getTimeZone() << ")";
|
2017-11-05 05:32:22 +00:00
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
2020-10-13 10:59:43 +00:00
|
|
|
void DataTypeDateTime::serializeText(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const
|
2017-11-05 05:32:22 +00:00
|
|
|
{
|
2020-10-13 10:59:43 +00:00
|
|
|
auto value = assert_cast<const ColumnType &>(column).getData()[row_num];
|
|
|
|
switch (settings.date_time_output_format)
|
|
|
|
{
|
|
|
|
case FormatSettings::DateTimeOutputFormat::Simple:
|
|
|
|
writeDateTimeText(value, ostr, time_zone);
|
|
|
|
return;
|
|
|
|
case FormatSettings::DateTimeOutputFormat::UnixTimestamp:
|
|
|
|
writeIntText(value, ostr);
|
|
|
|
return;
|
|
|
|
case FormatSettings::DateTimeOutputFormat::ISO:
|
|
|
|
writeDateTimeTextISO(value, ostr, utc_time_zone);
|
|
|
|
return;
|
|
|
|
}
|
2019-04-01 16:18:13 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 05:53:38 +00:00
|
|
|
void DataTypeDateTime::serializeTextEscaped(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const
|
2017-03-12 10:13:45 +00:00
|
|
|
{
|
2018-06-08 01:51:55 +00:00
|
|
|
serializeText(column, row_num, ostr, settings);
|
2017-03-12 10:13:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 05:53:38 +00:00
|
|
|
void DataTypeDateTime::deserializeWholeText(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const
|
2019-06-15 15:56:55 +00:00
|
|
|
{
|
|
|
|
deserializeTextEscaped(column, istr, settings);
|
|
|
|
}
|
|
|
|
|
2019-10-02 05:53:38 +00:00
|
|
|
void DataTypeDateTime::deserializeTextEscaped(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const
|
2017-03-12 10:13:45 +00:00
|
|
|
{
|
2019-10-02 05:53:38 +00:00
|
|
|
time_t x;
|
|
|
|
::readText(x, istr, settings, time_zone, utc_time_zone);
|
2019-10-03 02:07:36 +00:00
|
|
|
assert_cast<ColumnType &>(column).getData().push_back(x);
|
2017-03-12 10:13:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 05:53:38 +00:00
|
|
|
void DataTypeDateTime::serializeTextQuoted(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const
|
2017-03-12 10:13:45 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeChar('\'', ostr);
|
2018-06-08 01:51:55 +00:00
|
|
|
serializeText(column, row_num, ostr, settings);
|
2017-04-01 07:20:54 +00:00
|
|
|
writeChar('\'', ostr);
|
2017-03-12 10:13:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 05:53:38 +00:00
|
|
|
void DataTypeDateTime::deserializeTextQuoted(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const
|
2017-03-12 10:13:45 +00:00
|
|
|
{
|
2019-10-02 05:53:38 +00:00
|
|
|
time_t x;
|
2018-02-06 20:10:49 +00:00
|
|
|
if (checkChar('\'', istr)) /// Cases: '2017-08-31 18:36:48' or '1504193808'
|
|
|
|
{
|
2019-10-02 05:53:38 +00:00
|
|
|
::readText(x, istr, settings, time_zone, utc_time_zone);
|
2018-02-06 20:10:49 +00:00
|
|
|
assertChar('\'', istr);
|
|
|
|
}
|
|
|
|
else /// Just 1504193808 or 01504193808
|
|
|
|
{
|
|
|
|
readIntText(x, istr);
|
|
|
|
}
|
2019-10-03 02:07:36 +00:00
|
|
|
assert_cast<ColumnType &>(column).getData().push_back(x); /// It's important to do this at the end - for exception safety.
|
2017-03-12 10:13:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 05:53:38 +00:00
|
|
|
void DataTypeDateTime::serializeTextJSON(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const
|
2017-03-12 10:13:45 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeChar('"', ostr);
|
2018-06-08 01:51:55 +00:00
|
|
|
serializeText(column, row_num, ostr, settings);
|
2017-04-01 07:20:54 +00:00
|
|
|
writeChar('"', ostr);
|
2017-03-12 10:13:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 05:53:38 +00:00
|
|
|
void DataTypeDateTime::deserializeTextJSON(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const
|
2017-03-12 10:13:45 +00:00
|
|
|
{
|
2019-10-02 05:53:38 +00:00
|
|
|
time_t x;
|
2018-02-06 20:10:49 +00:00
|
|
|
if (checkChar('"', istr))
|
2017-08-31 15:42:15 +00:00
|
|
|
{
|
2019-10-02 05:53:38 +00:00
|
|
|
::readText(x, istr, settings, time_zone, utc_time_zone);
|
2017-08-31 15:42:15 +00:00
|
|
|
assertChar('"', istr);
|
|
|
|
}
|
2018-02-06 20:10:49 +00:00
|
|
|
else
|
2017-08-31 15:42:15 +00:00
|
|
|
{
|
|
|
|
readIntText(x, istr);
|
|
|
|
}
|
2019-10-03 02:07:36 +00:00
|
|
|
assert_cast<ColumnType &>(column).getData().push_back(x);
|
2017-03-12 10:13:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 05:53:38 +00:00
|
|
|
void DataTypeDateTime::serializeTextCSV(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const
|
2017-03-12 10:13:45 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeChar('"', ostr);
|
2018-06-08 01:51:55 +00:00
|
|
|
serializeText(column, row_num, ostr, settings);
|
2017-04-01 07:20:54 +00:00
|
|
|
writeChar('"', ostr);
|
2017-03-12 10:13:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 05:53:38 +00:00
|
|
|
void DataTypeDateTime::deserializeTextCSV(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const
|
2017-03-12 10:13:45 +00:00
|
|
|
{
|
2019-10-02 05:53:38 +00:00
|
|
|
time_t x;
|
2018-06-08 02:56:37 +00:00
|
|
|
|
|
|
|
if (istr.eof())
|
|
|
|
throwReadAfterEOF();
|
|
|
|
|
|
|
|
char maybe_quote = *istr.position();
|
|
|
|
|
|
|
|
if (maybe_quote == '\'' || maybe_quote == '\"')
|
|
|
|
++istr.position();
|
|
|
|
|
2019-10-02 05:53:38 +00:00
|
|
|
::readText(x, istr, settings, time_zone, utc_time_zone);
|
2018-06-08 02:56:37 +00:00
|
|
|
|
|
|
|
if (maybe_quote == '\'' || maybe_quote == '\"')
|
|
|
|
assertChar(maybe_quote, istr);
|
|
|
|
|
2019-10-03 02:07:36 +00:00
|
|
|
assert_cast<ColumnType &>(column).getData().push_back(x);
|
2017-03-12 10:13:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 05:53:38 +00:00
|
|
|
bool DataTypeDateTime::equals(const IDataType & rhs) const
|
|
|
|
{
|
|
|
|
/// DateTime with different timezones are equal, because:
|
|
|
|
/// "all types with different time zones are equivalent and may be used interchangingly."
|
|
|
|
return typeid(rhs) == typeid(*this);
|
|
|
|
}
|
2019-09-26 15:12:40 +00:00
|
|
|
|
2017-03-12 10:13:45 +00:00
|
|
|
}
|