-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Time zone related be function #1598
Conversation
this.queryGlobals.setNow_string(DATE_FORMAT.format(new Date())); | ||
this.queryGlobals.setNow_string(String.valueOf(new Date().getTime())); | ||
if (context.getSessionVariable().getTimeZone().equals("CST")) { | ||
this.queryGlobals.setTime_zone("Asia/Shanghai"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should add "Asia/Shanghai" to some definition class
be/src/runtime/timestamp_value.h
Outdated
|
||
void to_datetime_val(doris_udf::DateTimeVal *tv) const { | ||
boost::posix_time::ptime p = boost::posix_time::from_time_t(val / 1000); | ||
int _year = p.date().year(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your code style is not compatible with Doris C++ code style.
private member should starts with _
.
local variables should not start with _
be/src/runtime/timestamp_value.h
Outdated
val = timestamp; | ||
} | ||
|
||
bool from_date_time_value(const DateTimeValue& tv, std::string timezone) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should add some comments for these functions
be/src/runtime/timestamp_value.h
Outdated
static boost::local_time::tz_database _s_tz_database; | ||
}; | ||
|
||
class TimestampValue { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we just implement a constructor with (timestamp and timezone) in DataTimeValue is enough. And we can reuse code of DateTimeValue
…to time-zone-be-function Conflicts: be/src/exprs/timestamp_functions.cpp be/test/exprs/timestamp_functions_test.cpp fe/src/main/java/org/apache/doris/qe/Coordinator.java
5b72023
to
460c2d5
Compare
460c2d5
to
68357f9
Compare
be/src/testutil/function_utils.cpp
Outdated
@@ -34,6 +34,15 @@ FunctionUtils::FunctionUtils() { | |||
_fn_ctx = FunctionContextImpl::create_context( | |||
_state, _memory_pool, return_type, arg_types, 0, false); | |||
} | |||
FunctionUtils::FunctionUtils(RuntimeState* state) { | |||
_state = state; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
4 spaces indent
be/src/runtime/runtime_state.h
Outdated
int64_t timestamp() const { | ||
return _timestamp; | ||
} | ||
const std::string timezone() const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const std::string timezone() const { | |
const std::string& timezone() const { |
be/src/runtime/datetime_value.h
Outdated
@@ -435,7 +436,8 @@ class DateTimeValue { | |||
} | |||
|
|||
int64_t second_diff(const DateTimeValue& rhs) const { | |||
return unix_timestamp() - rhs.unix_timestamp(); | |||
return unix_timestamp(TimezoneDatabase::default_time_zone) | |||
- rhs.unix_timestamp(TimezoneDatabase::default_time_zone); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For second_diff which is a literal compare, I think we should not introduce timezone. Which can bring some cost which isn't needed. Such as search if timezone exists.
And I think we can introduce another seconds such as seconds from 0000-01-01 00:00:00, which has no relation with time zone.
be/src/exprs/timestamp_functions.cpp
Outdated
@@ -470,7 +372,8 @@ DoubleVal TimestampFunctions::time_diff( | |||
} | |||
const DateTimeValue& ts_value1 = DateTimeValue::from_datetime_val(ts_val1); | |||
const DateTimeValue& ts_value2 = DateTimeValue::from_datetime_val(ts_val2); | |||
int64_t timediff = ts_value1.unix_timestamp() - ts_value2.unix_timestamp(); | |||
int64_t timediff = ts_value1.unix_timestamp(ctx->impl()->state()->timezone()) | |||
- ts_value2.unix_timestamp(ctx->impl()->state()->timezone()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use another method to do this?
be/src/exprs/timezone_db.h
Outdated
~TimezoneDatabase(); | ||
|
||
static void init() { | ||
TimezoneDatabase(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what this mean?
I think what you need is not this TimezoneDatabase constructor.
You can move TimezoneDatabase()
to this init()
.
I think there is no need to have object of this class. we only just need some static functions
be/src/runtime/datetime_value.h
Outdated
@@ -332,9 +333,9 @@ class DateTimeValue { | |||
// Add interval | |||
bool date_add_interval(const TimeInterval& interval, TimeUnit unit); | |||
|
|||
int unix_timestamp() const; | |||
int64_t unix_timestamp(const std::string& timezone) const; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add some comments for these two functions
be/src/exprs/timestamp_functions.cpp
Outdated
} | ||
DateTimeVal TimestampFunctions::now(FunctionContext* context) { | ||
DateTimeValue dtv; | ||
dtv.from_unixtime(context->impl()->state()->timestamp() / 1000, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check this function's return?
be/src/exprs/timestamp_functions.cpp
Outdated
} | ||
DoubleVal TimestampFunctions::curtime(FunctionContext* context) { | ||
DateTimeValue dtv; | ||
dtv.from_unixtime(context->impl()->state()->timestamp() / 1000, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check return value?
be/src/runtime/datetime_value.cpp
Outdated
int DateTimeValue::unix_timestamp() const { | ||
int64_t days = daynr() - calc_daynr(1970, 1, 1); | ||
if (days < 0) { | ||
int64_t DateTimeValue::unix_timestamp(const std::string& timezone) const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should return a bool value if timezone is not valid
8b74260
to
9a22e98
Compare
be/src/runtime/datetime_value.h
Outdated
|
||
//unix_timestamp is called with a timezone argument, | ||
//it returns seconds of the value of date literal since '1970-01-01 00:00:00' UTC | ||
bool unix_timestamp(int64_t& timestamp, const std::string& timezone) const; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bool unix_timestamp(int64_t& timestamp, const std::string& timezone) const; | |
bool to_unix_timestamp(int64_t* timestamp, const std::string& timezone) const; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
9a22e98
to
fc06993
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Time zone related be function
#1583