Skip to content

Commit

Permalink
Flash-473 optimize date and datetime comparison (#221)
Browse files Browse the repository at this point in the history
* support udf like with 3 arguments

* address comments

* add some comments

* Flash-473 optimize date and datetime comparison

* address comments
  • Loading branch information
windtalker authored and zanmato1984 committed Sep 5, 2019
1 parent 8a0fb66 commit ff9a1de
Showing 1 changed file with 57 additions and 22 deletions.
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

0 comments on commit ff9a1de

Please sign in to comment.