Unix timestamp format for DateTime fields in CSV. Resolves #366. [#CLICKHOUSE-3168]

This commit is contained in:
Vitaliy Lyudvichenko 2017-08-15 13:20:05 +03:00 committed by alexey-milovidov
parent dc29ae7e73
commit ad40104022
4 changed files with 25 additions and 6 deletions

View File

@ -72,9 +72,9 @@ void DataTypeDateTime::serializeTextCSV(const IColumn & column, size_t row_num,
void DataTypeDateTime::deserializeTextCSV(IColumn & column, ReadBuffer & istr, const char delimiter) const
{
LocalDateTime value;
readCSV(value, istr);
static_cast<ColumnUInt32 &>(column).getData().push_back(static_cast<time_t>(value));
time_t x;
readCSVSimple(x, istr, readDateTimeText);
static_cast<ColumnUInt32 &>(column).getData().push_back(x);
}
void registerDataTypeDateTime(DataTypeFactory & factory)

View File

@ -629,7 +629,7 @@ void readDateTimeTextFallback(time_t & datetime, ReadBuffer & buf, const DateLUT
/** In YYYY-MM-DD hh:mm:ss format, according to specified time zone.
* As an exception, also supported parsing of unix timestamp in form of decimal number.
*/
inline void readDateTimeText(time_t & datetime, ReadBuffer & buf, const DateLUTImpl & date_lut = DateLUT::instance())
inline void readDateTimeText(time_t & datetime, ReadBuffer & buf, const DateLUTImpl & date_lut)
{
/** Read 10 characters, that could represent unix timestamp.
* Only unix timestamp of 5-10 characters is supported.
@ -666,6 +666,11 @@ inline void readDateTimeText(time_t & datetime, ReadBuffer & buf, const DateLUTI
readDateTimeTextFallback(datetime, buf, date_lut);
}
inline void readDateTimeText(time_t & datetime, ReadBuffer & buf)
{
readDateTimeText(datetime, buf, DateLUT::instance());
}
inline void readDateTimeText(LocalDateTime & datetime, ReadBuffer & buf)
{
char s[19];
@ -767,7 +772,7 @@ inline void readDoubleQuoted(LocalDateTime & x, ReadBuffer & buf)
/// CSV, for numbers, dates, datetimes: quotes are optional, no special escaping rules.
template <typename T>
inline void readCSVSimple(T & x, ReadBuffer & buf)
inline void readCSVSimple(T & x, ReadBuffer & buf, void (*readText_)(T & x, ReadBuffer & buf) = readText)
{
if (buf.eof())
throwReadAfterEOF();
@ -777,7 +782,7 @@ inline void readCSVSimple(T & x, ReadBuffer & buf)
if (maybe_quote == '\'' || maybe_quote == '\"')
++buf.position();
readText(x, buf);
readText_(x, buf);
if (maybe_quote == '\'' || maybe_quote == '\"')
assertChar(maybe_quote, buf);

View File

@ -2,3 +2,7 @@ Hello, world 123 2016-01-01
Hello, "world" 456 2016-01-02
Hello "world" 789 2016-01-03
Hello\n world 100 2016-01-04
2016-01-01 01:02:03 1
2016-01-02 01:02:03 2
2017-08-15 13:15:01 3
1970-01-02 06:46:39 4

View File

@ -11,3 +11,13 @@ Hello "world", 789 ,2016-01-03
clickhouse-client --query="SELECT * FROM test.csv ORDER BY d";
clickhouse-client --query="DROP TABLE test.csv";
clickhouse-client --query="CREATE TABLE test.csv (t DateTime, s String) ENGINE = Memory";
echo '"2016-01-01 01:02:03","1"
2016-01-02 01:02:03, "2"
1502792101,"3"
99999,"4"' | clickhouse-client --query="INSERT INTO test.csv FORMAT CSV";
clickhouse-client --query="SELECT * FROM test.csv ORDER BY s";
clickhouse-client --query="DROP TABLE test.csv";