Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flash-473 optimize date and datetime comparison #221

Merged
merged 6 commits into from
Sep 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 57 additions & 22 deletions dbms/src/Functions/FunctionsComparison.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ inline time_t dateToDateTime(UInt32 date_data)
return DateLUT::instance().makeDateTime(local_date.year(), local_date.month(), local_date.day(), 0, 0, 0);
}

inline std::tuple<DayNum_t, bool> dateTimeToDate(time_t time_data)
{
// todo use timezone info
auto & date_lut = DateLUT::instance();
auto truncated = date_lut.toHour(time_data) != 0 || date_lut.toMinute(time_data) != 0 || date_lut.toSecond(time_data) != 0;
auto values = date_lut.getValues(time_data);
auto day_num = date_lut.makeDayNum(values.year, values.month, values.day_of_month);
return std::make_tuple(day_num, truncated);
}


template <typename A, typename B, template <typename, typename> class Op, bool is_left_date>
struct DateDateTimeComparisonImpl
Expand Down Expand Up @@ -175,18 +185,31 @@ struct DateDateTimeComparisonImpl
}
else
{
using OpType = B;
size_t size = a.size();
const A * a_pos = &a[0];
UInt8 * c_pos = &c[0];
const A * a_end = a_pos + size;

while (a_pos < a_end)
// date vector with datetime constant
// first check if datetime constant can be convert to date constant
bool truncated;
DayNum_t date_num;
std::tie(date_num, truncated) = dateTimeToDate((time_t) b);
if (!truncated)
{
time_t date_time = dateToDateTime(*a_pos);
*c_pos = Op<OpType, OpType>::apply((OpType)date_time, b);
++a_pos;
++c_pos;
using OpType = A;
NumComparisonImpl<OpType, OpType, Op<OpType, OpType>>::vector_constant(a, (OpType) date_num, c);
}
else
{
using OpType = B;
size_t size = a.size();
const A *a_pos = &a[0];
UInt8 *c_pos = &c[0];
const A *a_end = a_pos + size;

while (a_pos < a_end)
{
time_t date_time = dateToDateTime(*a_pos);
*c_pos = Op<OpType, OpType>::apply((OpType) date_time, b);
++a_pos;
++c_pos;
}
}
}
}
Expand All @@ -202,18 +225,30 @@ struct DateDateTimeComparisonImpl
}
else
{
using OpType = A;
size_t size = b.size();
const B * b_pos = &b[0];
UInt8 * c_pos = &c[0];
const B * b_end = b_pos + size;

while (b_pos < b_end)
// datetime constant with date vector
bool truncated;
DayNum_t date_num;
std::tie(date_num, truncated) = dateTimeToDate((time_t) a);
if (!truncated)
{
time_t date_time = dateToDateTime(*b_pos);
*c_pos = Op<OpType, OpType>::apply(a, (OpType)date_time);
++b_pos;
++c_pos;
using OpType = B;
NumComparisonImpl<OpType, OpType, Op<OpType, OpType>>::vector_constant((OpType)a, date_num, c);
}
else
{
using OpType = A;
size_t size = b.size();
const B *b_pos = &b[0];
UInt8 *c_pos = &c[0];
const B *b_end = b_pos + size;

while (b_pos < b_end)
{
time_t date_time = dateToDateTime(*b_pos);
*c_pos = Op<OpType, OpType>::apply(a, (OpType) date_time);
++b_pos;
++c_pos;
}
}
}
}
Expand Down