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>
|
2019-02-19 20:01:31 +00:00
|
|
|
#include <Formats/ProtobufReader.h>
|
2019-01-23 19:41:18 +00:00
|
|
|
#include <Formats/ProtobufWriter.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();
|
|
|
|
}
|
|
|
|
|
2019-10-02 05:53:38 +00:00
|
|
|
void DataTypeDateTime::serializeText(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const
|
2017-11-05 05:32:22 +00:00
|
|
|
{
|
2019-10-03 02:07:36 +00:00
|
|
|
writeDateTimeText(assert_cast<const ColumnType &>(column).getData()[row_num], ostr, time_zone);
|
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
|
|
|
void DataTypeDateTime::serializeProtobuf(const IColumn & column, size_t row_num, ProtobufWriter & protobuf, size_t & value_index) const
|
2019-01-23 19:41:18 +00:00
|
|
|
{
|
2019-11-07 10:56:13 +00:00
|
|
|
if (value_index)
|
|
|
|
return;
|
2019-11-13 09:45:59 +00:00
|
|
|
|
|
|
|
// On some platforms `time_t` is `long` but not `unsigned int` (UInt32 that we store in column), hence static_cast.
|
|
|
|
value_index = static_cast<bool>(protobuf.writeDateTime(static_cast<time_t>(assert_cast<const ColumnType &>(column).getData()[row_num])));
|
2019-01-23 19:41:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 05:53:38 +00:00
|
|
|
void DataTypeDateTime::deserializeProtobuf(IColumn & column, ProtobufReader & protobuf, bool allow_add_row, bool & row_added) const
|
2019-02-19 20:01:31 +00:00
|
|
|
{
|
|
|
|
row_added = false;
|
2019-10-02 05:53:38 +00:00
|
|
|
time_t t;
|
|
|
|
if (!protobuf.readDateTime(t))
|
2019-02-19 20:01:31 +00:00
|
|
|
return;
|
|
|
|
|
2019-10-03 02:07:36 +00:00
|
|
|
auto & container = assert_cast<ColumnType &>(column).getData();
|
2019-02-19 20:01:31 +00:00
|
|
|
if (allow_add_row)
|
|
|
|
{
|
|
|
|
container.emplace_back(t);
|
|
|
|
row_added = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
container.back() = t;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|