Skip to content

Conversation

@HangyuanLiu
Copy link
Contributor

Time zone related be function
#1583

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");
Copy link
Contributor

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


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();
Copy link
Contributor

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 _

val = timestamp;
}

bool from_date_time_value(const DateTimeValue& tv, std::string timezone) {
Copy link
Contributor

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

static boost::local_time::tz_database _s_tz_database;
};

class TimestampValue {
Copy link
Contributor

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
@HangyuanLiu HangyuanLiu force-pushed the time-zone-be-function branch 2 times, most recently from 5b72023 to 460c2d5 Compare August 9, 2019 09:09
@HangyuanLiu HangyuanLiu force-pushed the time-zone-be-function branch from 460c2d5 to 68357f9 Compare August 9, 2019 09:13
_state, _memory_pool, return_type, arg_types, 0, false);
}
FunctionUtils::FunctionUtils(RuntimeState* state) {
_state = state;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 spaces indent

int64_t timestamp() const {
return _timestamp;
}
const std::string timezone() const {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const std::string timezone() const {
const std::string& timezone() const {

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);
Copy link
Contributor

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.

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());
Copy link
Contributor

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?

~TimezoneDatabase();

static void init() {
TimezoneDatabase();
Copy link
Contributor

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

bool date_add_interval(const TimeInterval& interval, TimeUnit unit);

int unix_timestamp() const;
int64_t unix_timestamp(const std::string& timezone) const;
Copy link
Contributor

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

}
DateTimeVal TimestampFunctions::now(FunctionContext* context) {
DateTimeValue dtv;
dtv.from_unixtime(context->impl()->state()->timestamp() / 1000,
Copy link
Contributor

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?

}
DoubleVal TimestampFunctions::curtime(FunctionContext* context) {
DateTimeValue dtv;
dtv.from_unixtime(context->impl()->state()->timestamp() / 1000,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check return value?

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 {
Copy link
Contributor

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

@HangyuanLiu HangyuanLiu force-pushed the time-zone-be-function branch 2 times, most recently from 8b74260 to 9a22e98 Compare August 12, 2019 08:19

//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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bool unix_timestamp(int64_t& timestamp, const std::string& timezone) const;
bool to_unix_timestamp(int64_t* timestamp, const std::string& timezone) const;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@HangyuanLiu HangyuanLiu force-pushed the time-zone-be-function branch from 9a22e98 to fc06993 Compare August 12, 2019 08:40
Copy link
Contributor

@imay imay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@morningman morningman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@morningman morningman merged commit 69af50a into apache:master Aug 12, 2019
@imay imay mentioned this pull request Sep 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants