dbms: allowed to compare Date and DateTime with strings in IN [#METR-2944].

This commit is contained in:
Alexey Milovidov 2015-06-16 21:50:44 +03:00
parent 26813667eb
commit 5c7dccebc1
3 changed files with 46 additions and 4 deletions

View File

@ -336,11 +336,37 @@ static Field convertToType(const Field & src, const IDataType & type)
}
else if (is_date || is_datetime)
{
if (src.getType() != Field::Types::UInt64)
throw Exception("Type mismatch in IN section: " + type.getName() + " at left, "
+ Field::Types::toString(src.getType()) + " at right");
if (src.getType() == Field::Types::UInt64)
return src;
return src;
if (src.getType() == Field::Types::String)
{
/// Возможность сравнивать даты и даты-с-временем со строкой.
const String & str = src.get<const String &>();
ReadBufferFromString in(str);
if (is_date)
{
DayNum_t date{};
readDateText(date, in);
if (!in.eof())
throw Exception("String is too long for Date: " + str);
return Field(UInt64(date));
}
else
{
time_t date_time{};
readDateTimeText(date_time, in);
if (!in.eof())
throw Exception("String is too long for DateTime: " + str);
return Field(UInt64(date_time));
}
}
throw Exception("Type mismatch in IN section: " + type.getName() + " at left, "
+ Field::Types::toString(src.getType()) + " at right");
}
}
else

View File

@ -0,0 +1,8 @@
SELECT toDate('2015-02-05') IN ('2015-02-04', '2015-02-05');
SELECT toDate('2015-02-05') IN ('2015-02-04', '2015-02-06');
SELECT toDateTime('2015-02-03 04:05:06') IN ('2015-02-03 04:05:06', '2015-02-03 05:06:07');
SELECT toDateTime('2015-02-03 04:05:06') IN ('2015-02-04 04:05:06', '2015-02-03 05:06:07');
SELECT toDate('2015-02-05') NOT IN ('2015-02-04', '2015-02-05');
SELECT toDate('2015-02-05') NOT IN ('2015-02-04', '2015-02-06');
SELECT toDateTime('2015-02-03 04:05:06') NOT IN ('2015-02-03 04:05:06', '2015-02-03 05:06:07');
SELECT toDateTime('2015-02-03 04:05:06') NOT IN ('2015-02-04 04:05:06', '2015-02-03 05:06:07');