Skip to content

Commit

Permalink
[refine](time) use TimeValue to refine repetitive logic and remove un…
Browse files Browse the repository at this point in the history
…used code. (apache#42978)
  • Loading branch information
Mryange authored Nov 11, 2024
1 parent 14084ee commit a8999f5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 58 deletions.
27 changes: 0 additions & 27 deletions be/src/vec/data_types/serde/data_type_time_serde.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,6 @@
namespace doris {
namespace vectorized {

template <bool is_binary_format>
Status DataTypeTimeSerDe::_write_column_to_mysql(const IColumn& column,
MysqlRowBuffer<is_binary_format>& result,
int row_idx, bool col_const,
const FormatOptions& options) const {
auto& data = assert_cast<const ColumnFloat64&>(column).get_data();
const auto col_index = index_check_const(row_idx, col_const);
if (UNLIKELY(0 != result.push_time(data[col_index]))) {
return Status::InternalError("pack mysql buffer failed.");
}
return Status::OK();
}

Status DataTypeTimeSerDe::write_column_to_mysql(const IColumn& column,
MysqlRowBuffer<true>& row_buffer, int row_idx,
bool col_const,
const FormatOptions& options) const {
return _write_column_to_mysql(column, row_buffer, row_idx, col_const, options);
}

Status DataTypeTimeSerDe::write_column_to_mysql(const IColumn& column,
MysqlRowBuffer<false>& row_buffer, int row_idx,
bool col_const,
const FormatOptions& options) const {
return _write_column_to_mysql(column, row_buffer, row_idx, col_const, options);
}

Status DataTypeTimeV2SerDe::write_column_to_mysql(const IColumn& column,
MysqlRowBuffer<true>& row_buffer, int row_idx,
bool col_const,
Expand Down
18 changes: 0 additions & 18 deletions be/src/vec/data_types/serde/data_type_time_serde.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,6 @@ namespace doris {
class JsonbOutStream;

namespace vectorized {
class Arena;

class DataTypeTimeSerDe : public DataTypeNumberSerDe<Float64> {
public:
DataTypeTimeSerDe(int nesting_level = 1) : DataTypeNumberSerDe<Float64>(nesting_level) {};

Status write_column_to_mysql(const IColumn& column, MysqlRowBuffer<true>& row_buffer,
int row_idx, bool col_const,
const FormatOptions& options) const override;
Status write_column_to_mysql(const IColumn& column, MysqlRowBuffer<false>& row_buffer,
int row_idx, bool col_const,
const FormatOptions& options) const override;

private:
template <bool is_binary_format>
Status _write_column_to_mysql(const IColumn& column, MysqlRowBuffer<is_binary_format>& result,
int row_idx, bool col_const, const FormatOptions& options) const;
};
class DataTypeTimeV2SerDe : public DataTypeNumberSerDe<Float64> {
public:
DataTypeTimeV2SerDe(int scale = 0, int nesting_level = 1)
Expand Down
11 changes: 5 additions & 6 deletions be/src/vec/functions/function_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
#include "vec/functions/function_convert_tz.h"
#include "vec/functions/function_helpers.h"
#include "vec/io/reader_buffer.h"
#include "vec/runtime/time_value.h"
#include "vec/runtime/vdatetime_value.h"
#include "vec/utils/util.hpp"

Expand Down Expand Up @@ -145,7 +146,7 @@ struct TimeCast {
} else {
if (VecDateTimeValue dv {}; dv.from_date_str(s, len, local_time_zone)) {
// can be parse as a datetime
x = dv.hour() * 3600 + dv.minute() * 60 + dv.second();
x = TimeValue::make_time(dv.hour(), dv.minute(), dv.second());
return true;
}
return false;
Expand Down Expand Up @@ -210,7 +211,7 @@ struct TimeCast {
if (minute >= 60 || second >= 60) {
return false;
}
x = hour * 3600 + minute * 60 + second;
x = TimeValue::make_time(hour, minute, second);
return true;
}
// Cast from number
Expand All @@ -227,7 +228,7 @@ struct TimeCast {
if (minute >= 60 || second >= 60) {
return false;
}
x = hour * 3600 + minute * 60 + second;
x = TimeValue::make_time(hour, minute, second);
return true;
}
template <typename S>
Expand All @@ -243,7 +244,7 @@ struct TimeCast {
if (minute >= 60 || second >= 60) {
return false;
}
x = hour * 3600 + minute * 60 + second;
x = TimeValue::make_time(hour, minute, second);
return true;
}
};
Expand Down Expand Up @@ -428,7 +429,6 @@ struct ConvertImpl {
for (size_t i = 0; i < size; ++i) {
(*vec_null_map_to)[i] = !TimeCast::try_parse_time(
vec_from[i], vec_to[i], context->state()->timezone_obj());
vec_to[i] *= (1000 * 1000);
}
block.get_by_position(result).column =
ColumnNullable::create(std::move(col_to), std::move(col_null_map_to));
Expand Down Expand Up @@ -1061,7 +1061,6 @@ bool try_parse_impl(typename DataType::FieldType& x, ReadBuffer& rb, FunctionCon
auto s = rb.position();
rb.position() = rb.end(); // make is_all_read = true
auto ret = TimeCast::try_parse_time(s, len, x, context->state()->timezone_obj());
x *= (1000 * 1000);
return ret;
}
if constexpr (std::is_floating_point_v<typename DataType::FieldType>) {
Expand Down
14 changes: 7 additions & 7 deletions be/src/vec/functions/function_date_or_datetime_computation.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include "vec/data_types/data_type_time_v2.h"
#include "vec/functions/function.h"
#include "vec/functions/function_helpers.h"
#include "vec/runtime/time_value.h"
#include "vec/runtime/vdatetime_value.h"
#include "vec/utils/util.hpp"

Expand Down Expand Up @@ -307,7 +308,7 @@ struct TimeDiffImpl {
return (double)diff_m;
}
} else {
return (double)((1000 * 1000) * ts0.second_diff(ts1));
return TimeValue::from_second(ts0.second_diff(ts1));
}
}
static DataTypes get_variadic_argument_types() {
Expand Down Expand Up @@ -993,9 +994,8 @@ struct CurrentTimeImpl {
VecDateTimeValue dtv;
dtv.from_unixtime(context->state()->timestamp_ms() / 1000,
context->state()->timezone_obj());
double time = dtv.hour() * 3600l + dtv.minute() * 60l + dtv.second();
time *= (1000 * 1000);
col_to->insert_data(const_cast<const char*>(reinterpret_cast<char*>(&time)), 0);
auto time = TimeValue::make_time(dtv.hour(), dtv.minute(), dtv.second());
col_to->insert_value(time);
block.get_by_position(result).column =
ColumnConst::create(std::move(col_to), input_rows_count);
return Status::OK();
Expand All @@ -1014,8 +1014,8 @@ struct TimeToSecImpl {

auto& res_data = res_col->get_data();
for (int i = 0; i < input_rows_count; ++i) {
res_data[i] =
cast_set<int>(static_cast<int64_t>(column_data.get_element(i)) / (1000 * 1000));
res_data[i] = cast_set<int>(static_cast<int64_t>(column_data.get_element(i)) /
(TimeValue::ONE_SECOND_MICROSECONDS));
}
block.replace_by_position(result, std::move(res_col));

Expand All @@ -1034,7 +1034,7 @@ struct SecToTimeImpl {
auto res_col = ColumnFloat64::create(input_rows_count);
auto& res_data = res_col->get_data();
for (int i = 0; i < input_rows_count; ++i) {
res_data[i] = (1000 * 1000) * static_cast<double>(column_data.get_element(i));
res_data[i] = TimeValue::from_second(column_data.get_element(i));
}

block.replace_by_position(result, std::move(res_col));
Expand Down
11 changes: 11 additions & 0 deletions be/src/vec/runtime/time_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ class TimeValue {
return static_cast<int64_t>(time);
}

static TimeType make_time(int64_t hour, int64_t minute, int64_t second,
int64_t microsecond = 0) {
int64_t value = hour * ONE_HOUR_MICROSECONDS + minute * ONE_MINUTE_MICROSECONDS +
second * ONE_SECOND_MICROSECONDS + microsecond;
return static_cast<TimeType>(value);
}

static std::string to_string(TimeType time, int scale) {
return timev2_to_buffer_from_double(time, scale);
}
Expand All @@ -70,6 +77,10 @@ class TimeValue {
static int32_t second(TimeType time) {
return (check_over_max_time(time) / ONE_SECOND_MICROSECONDS) % ONE_MINUTE_SECONDS;
}

static TimeType from_second(int64_t sec) {
return static_cast<TimeType>(sec * ONE_SECOND_MICROSECONDS);
}
};

} // namespace doris
Expand Down

0 comments on commit a8999f5

Please sign in to comment.