imay commented on a change in pull request #1598: Time zone related be function
URL: https://github.com/apache/incubator-doris/pull/1598#discussion_r312797672
##########
File path: be/src/exprs/timestamp_functions.cpp
##########
@@ -485,142 +385,112 @@ IntVal TimestampFunctions::date_diff(
return IntVal(ts_value1.daynr() - ts_value2.daynr());
}
+// TimeZone correlation functions.
DateTimeVal TimestampFunctions::timestamp(
FunctionContext* ctx, const DateTimeVal& val) {
return val;
}
-void* TimestampFunctions::from_utc(Expr* e, TupleRow* row) {
- return NULL;
- // DCHECK_EQ(e->get_num_children(), 2);
- // Expr* op1 = e->children()[0];
- // Expr* op2 = e->children()[1];
- // DateTimeValue* tv =
reinterpret_cast<DateTimeValue*>(op1->get_value(row));
- // StringValue* tz = reinterpret_cast<StringValue*>(op2->get_value(row));
-
- // if (tv == NULL || tz == NULL) {
- // return NULL;
- // }
-
- // if (tv->not_a_date_time()) {
- // return NULL;
- // }
-
- // boost::local_time::time_zone_ptr timezone =
TimezoneDatabase::find_timezone(tz->debug_string());
-
- // This should raise some sort of error or at least null. Hive just
ignores it.
- // if (timezone == NULL) {
- // LOG(ERROR) << "Unknown timezone '" << *tz << "'" << std::endl;
- // e->_result.timestamp_val = *tv;
- // return &e->_result.timestamp_val;
- // }
-
- // boost::posix_time::ptime temp;
- // tv->to_ptime(&temp);
- // boost::local_time::local_date_time lt(temp, timezone);
- // e->_result.timestamp_val = lt.local_time();
- // return &e->_result.timestamp_val;
+StringVal TimestampFunctions::from_unix(
+ FunctionContext* context, const IntVal& unix_time) {
+ if (unix_time.is_null) {
+ return StringVal::null();
+ }
+ DateTimeValue dtv;
+ if (!dtv.from_unixtime(unix_time.val,
context->impl()->state()->timezone())) {
+ return StringVal::null();
+ }
+ char buf[64];
+ dtv.to_string(buf);
+ return AnyValUtil::from_string_temp(context, buf);
}
-void* TimestampFunctions::to_utc(Expr* e, TupleRow* row) {
- return NULL;
- // DCHECK_EQ(e->get_num_children(), 2);
- // Expr* op1 = e->children()[0];
- // Expr* op2 = e->children()[1];
- // DateTimeValue* tv =
reinterpret_cast<DateTimeValue*>(op1->get_value(row));
- // StringValue* tz = reinterpret_cast<StringValue*>(op2->get_value(row));
-
- // if (tv == NULL || tz == NULL) {
- // return NULL;
- // }
-
- // if (tv->not_a_date_time()) {
- // return NULL;
- // }
-
- // boost::local_time::time_zone_ptr timezone =
TimezoneDatabase::find_timezone(tz->debug_string());
-
- // This should raise some sort of error or at least null. Hive just
ignores it.
- // if (timezone == NULL) {
- // LOG(ERROR) << "Unknown timezone '" << *tz << "'" << std::endl;
- // e->_result.timestamp_val = *tv;
- // return &e->_result.timestamp_val;
- // }
+StringVal TimestampFunctions::from_unix(
+ FunctionContext* context, const IntVal& unix_time, const
StringVal& fmt) {
+ if (unix_time.is_null || fmt.is_null) {
+ return StringVal::null();
+ }
+ DateTimeValue dtv;
+ if (!dtv.from_unixtime(unix_time.val,
context->impl()->state()->timezone())) {
+ return StringVal::null();
+ }
- // boost::local_time::local_date_time lt(tv->date(), tv->time_of_day(),
- // timezone,
boost::local_time::local_date_time::NOT_DATE_TIME_ON_ERROR);
- // e->_result.timestamp_val = DateTimeValue(lt.utc_time());
- // return &e->_result.timestamp_val;
+ char buf[128];
+ if (!dtv.to_format_string((const char*)fmt.ptr, fmt.len, buf)) {
+ return StringVal::null();
+ }
+ return AnyValUtil::from_string_temp(context, buf);
}
-TimezoneDatabase::TimezoneDatabase() {
- // Create a temporary file and write the timezone information. The boost
- // interface only loads this format from a file. We don't want to raise
- // an error here since this is done when the backend is created and this
- // information might not actually get used by any queries.
- char filestr[] = "/tmp/doris.tzdb.XXXXXXX";
- FILE* file = NULL;
- int fd = -1;
+IntVal TimestampFunctions::to_unix(FunctionContext* context) {
+ return IntVal(context->impl()->state()->timestamp() / 1000);
+}
- if ((fd = mkstemp(filestr)) == -1) {
- LOG(ERROR) << "Could not create temporary timezone file: " << filestr;
- return;
+IntVal TimestampFunctions::to_unix(
+ FunctionContext* context, const StringVal& string_val, const
StringVal& fmt) {
+ if (string_val.is_null || fmt.is_null) {
+ return IntVal::null();
}
-
- if ((file = fopen(filestr, "w")) == NULL) {
- unlink(filestr);
- close(fd);
- LOG(ERROR) << "Could not open temporary timezone file: " << filestr;
- return;
+ DateTimeValue tv;
+ if (!tv.from_date_format_str(
+ (const char *)fmt.ptr, fmt.len, (const char *)string_val.ptr,
string_val.len)) {
+ return IntVal::null();
}
+ return tv.unix_timestamp(context->impl()->state()->timezone());
+}
- if (fputs(_s_timezone_database_str, file) == EOF) {
- unlink(filestr);
- close(fd);
- fclose(file);
- LOG(ERROR) << "Could not load temporary timezone file: " << filestr;
- return;
+IntVal TimestampFunctions::to_unix(
+ FunctionContext* context, const DateTimeVal& ts_val) {
+ if (ts_val.is_null) {
+ return IntVal::null();
}
-
- fclose(file);
- _s_tz_database.load_from_file(std::string(filestr));
- _s_tz_region_list = _s_tz_database.region_list();
- unlink(filestr);
- close(fd);
+ const DateTimeValue &tv = DateTimeValue::from_datetime_val(ts_val);
+ return tv.unix_timestamp(context->impl()->state()->timezone());
}
-TimezoneDatabase::~TimezoneDatabase() { }
-
-boost::local_time::time_zone_ptr TimezoneDatabase::find_timezone(const
std::string& tz) {
- // See if they specified a zone id
- if (tz.find_first_of('/') != std::string::npos) {
- return _s_tz_database.time_zone_from_region(tz);
- }
+DateTimeVal TimestampFunctions::utc_timestamp(FunctionContext* context) {
+ DateTimeValue dtv;
+ dtv.from_unixtime(context->impl()->state()->timestamp() / 1000, "+00:00");
- for (std::vector<std::string>::const_iterator iter =
_s_tz_region_list.begin();
- iter != _s_tz_region_list.end(); ++iter) {
- boost::local_time::time_zone_ptr tzp =
_s_tz_database.time_zone_from_region(*iter);
- DCHECK(tzp != NULL);
+ DateTimeVal return_val;
+ dtv.to_datetime_val(&return_val);
+ return return_val;
+}
- if (tzp->dst_zone_abbrev() == tz) {
- return tzp;
- }
+DateTimeVal TimestampFunctions::now(FunctionContext* context) {
+ DateTimeValue dtv;
+ dtv.from_unixtime(context->impl()->state()->timestamp() / 1000,
+ context->impl()->state()->timezone());
- if (tzp->std_zone_abbrev() == tz) {
- return tzp;
- }
+ DateTimeVal return_val;
+ dtv.to_datetime_val(&return_val);
+ return return_val;
+}
- if (tzp->dst_zone_name() == tz) {
- return tzp;
- }
+DoubleVal TimestampFunctions::curtime(FunctionContext* context) {
+ DateTimeValue dtv;
+ dtv.from_unixtime(context->impl()->state()->timestamp() / 1000,
Review comment:
check return value?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]