Skip to content

Commit

Permalink
Refactor TimestampConversion to return velox::Expected (facebookincub…
Browse files Browse the repository at this point in the history
…ator#10997)

Summary:
We have recently introduced velox::Expected (facebookincubator#9858) to use in non-void no-throw APIs. It's more proper to return that.

Pull Request resolved: facebookincubator#10997

Reviewed By: xiaoxmeng

Differential Revision: D63035456

Pulled By: Yuhta

fbshipit-source-id: 6c8322593f9677e1dfa70f719b2922429f418d57
  • Loading branch information
NEUpanning authored and facebook-github-bot committed Sep 20, 2024
1 parent 8c2fa20 commit 023be97
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 141 deletions.
24 changes: 12 additions & 12 deletions velox/functions/lib/DateTimeFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1423,27 +1423,27 @@ Expected<DateTimeResult> DateTimeFormatter::parse(
}

// Convert the parsed date/time into a timestamp.
int64_t daysSinceEpoch;
Status status;
Expected<int64_t> daysSinceEpoch;
if (date.weekDateFormat) {
status = util::daysSinceEpochFromWeekDate(
date.year, date.week, date.dayOfWeek, daysSinceEpoch);
daysSinceEpoch =
util::daysSinceEpochFromWeekDate(date.year, date.week, date.dayOfWeek);
} else if (date.dayOfYearFormat) {
status = util::daysSinceEpochFromDayOfYear(
date.year, date.dayOfYear, daysSinceEpoch);
daysSinceEpoch =
util::daysSinceEpochFromDayOfYear(date.year, date.dayOfYear);
} else {
status = util::daysSinceEpochFromDate(
date.year, date.month, date.day, daysSinceEpoch);
daysSinceEpoch =
util::daysSinceEpochFromDate(date.year, date.month, date.day);
}
if (!status.ok()) {
VELOX_DCHECK(status.isUserError());
return folly::makeUnexpected(status);
if (daysSinceEpoch.hasError()) {
VELOX_DCHECK(daysSinceEpoch.error().isUserError());
return folly::makeUnexpected(daysSinceEpoch.error());
}

int64_t microsSinceMidnight =
util::fromTime(date.hour, date.minute, date.second, date.microsecond);
return DateTimeResult{
util::fromDatetime(daysSinceEpoch, microsSinceMidnight), date.timezoneId};
util::fromDatetime(daysSinceEpoch.value(), microsSinceMidnight),
date.timezoneId};
}

std::shared_ptr<DateTimeFormatter> buildMysqlDateTimeFormatter(
Expand Down
39 changes: 18 additions & 21 deletions velox/functions/prestosql/DateTimeFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,43 +401,40 @@ struct LastDayOfMonthFunction : public InitSessionTimezone<T>,
out_type<Date>& result,
const arg_type<Timestamp>& timestamp) {
auto dt = getDateTime(timestamp, this->timeZone_);
int64_t daysSinceEpochFromDate;
auto status =
util::lastDayOfMonthSinceEpochFromDate(dt, daysSinceEpochFromDate);
if (!status.ok()) {
VELOX_DCHECK(status.isUserError());
VELOX_USER_FAIL(status.message());
Expected<int64_t> daysSinceEpochFromDate =
util::lastDayOfMonthSinceEpochFromDate(dt);
if (daysSinceEpochFromDate.hasError()) {
VELOX_DCHECK(daysSinceEpochFromDate.error().isUserError());
VELOX_USER_FAIL(daysSinceEpochFromDate.error().message());
}
result = daysSinceEpochFromDate;
result = daysSinceEpochFromDate.value();
}

FOLLY_ALWAYS_INLINE void call(
out_type<Date>& result,
const arg_type<Date>& date) {
auto dt = getDateTime(date);
int64_t daysSinceEpochFromDate;
auto status =
util::lastDayOfMonthSinceEpochFromDate(dt, daysSinceEpochFromDate);
if (!status.ok()) {
VELOX_DCHECK(status.isUserError());
VELOX_USER_FAIL(status.message());
Expected<int64_t> lastDayOfMonthSinceEpoch =
util::lastDayOfMonthSinceEpochFromDate(dt);
if (lastDayOfMonthSinceEpoch.hasError()) {
VELOX_DCHECK(lastDayOfMonthSinceEpoch.error().isUserError());
VELOX_USER_FAIL(lastDayOfMonthSinceEpoch.error().message());
}
result = daysSinceEpochFromDate;
result = lastDayOfMonthSinceEpoch.value();
}

FOLLY_ALWAYS_INLINE void call(
out_type<Date>& result,
const arg_type<TimestampWithTimezone>& timestampWithTimezone) {
auto timestamp = this->toTimestamp(timestampWithTimezone);
auto dt = getDateTime(timestamp, nullptr);
int64_t daysSinceEpochFromDate;
auto status =
util::lastDayOfMonthSinceEpochFromDate(dt, daysSinceEpochFromDate);
if (!status.ok()) {
VELOX_DCHECK(status.isUserError());
VELOX_USER_FAIL(status.message());
Expected<int64_t> lastDayOfMonthSinceEpoch =
util::lastDayOfMonthSinceEpochFromDate(dt);
if (lastDayOfMonthSinceEpoch.hasError()) {
VELOX_DCHECK(lastDayOfMonthSinceEpoch.error().isUserError());
VELOX_USER_FAIL(lastDayOfMonthSinceEpoch.error().message());
}
result = daysSinceEpochFromDate;
result = lastDayOfMonthSinceEpoch.value();
}
};

Expand Down
35 changes: 17 additions & 18 deletions velox/functions/sparksql/DateTimeFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,13 +415,12 @@ struct MakeDateFunction {
const int32_t year,
const int32_t month,
const int32_t day) {
int64_t daysSinceEpoch;
auto status =
util::daysSinceEpochFromDate(year, month, day, daysSinceEpoch);
if (!status.ok()) {
VELOX_DCHECK(status.isUserError());
VELOX_USER_FAIL(status.message());
Expected<int64_t> expected = util::daysSinceEpochFromDate(year, month, day);
if (expected.hasError()) {
VELOX_DCHECK(expected.error().isUserError());
VELOX_USER_FAIL(expected.error().message());
}
int64_t daysSinceEpoch = expected.value();
VELOX_USER_CHECK_EQ(
daysSinceEpoch,
(int32_t)daysSinceEpoch,
Expand Down Expand Up @@ -457,13 +456,13 @@ struct LastDayFunction {
int32_t month = getMonth(dateTime);
int32_t day = getMonth(dateTime);
auto lastDay = util::getMaxDayOfMonth(year, month);
int64_t daysSinceEpoch;
auto status =
util::daysSinceEpochFromDate(year, month, lastDay, daysSinceEpoch);
if (!status.ok()) {
VELOX_DCHECK(status.isUserError());
VELOX_USER_FAIL(status.message());
Expected<int64_t> expected =
util::daysSinceEpochFromDate(year, month, lastDay);
if (expected.hasError()) {
VELOX_DCHECK(expected.error().isUserError());
VELOX_USER_FAIL(expected.error().message());
}
int64_t daysSinceEpoch = expected.value();
VELOX_USER_CHECK_EQ(
daysSinceEpoch,
(int32_t)daysSinceEpoch,
Expand Down Expand Up @@ -567,13 +566,13 @@ struct AddMonthsFunction {
// Adjusts day to valid one.
auto dayResult = lastDayOfMonth < day ? lastDayOfMonth : day;

int64_t daysSinceEpoch;
auto status = util::daysSinceEpochFromDate(
yearResult, monthResult, dayResult, daysSinceEpoch);
if (!status.ok()) {
VELOX_DCHECK(status.isUserError());
VELOX_USER_FAIL(status.message());
Expected<int64_t> expected =
util::daysSinceEpochFromDate(yearResult, monthResult, dayResult);
if (expected.hasError()) {
VELOX_DCHECK(expected.error().isUserError());
VELOX_USER_FAIL(expected.error().message());
}
int64_t daysSinceEpoch = expected.value();
VELOX_USER_CHECK_EQ(
daysSinceEpoch,
(int32_t)daysSinceEpoch,
Expand Down
12 changes: 5 additions & 7 deletions velox/functions/sparksql/MakeTimestamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,19 @@ std::optional<Timestamp> makeTimeStampFromDecodedArgs(
}

// Year, month, day will be checked in utils::daysSinceEpochFromDate.
int64_t daysSinceEpoch;
auto status = util::daysSinceEpochFromDate(
Expected<int64_t> daysSinceEpoch = util::daysSinceEpochFromDate(
yearVector->valueAt<int32_t>(row),
monthVector->valueAt<int32_t>(row),
dayVector->valueAt<int32_t>(row),
daysSinceEpoch);
if (!status.ok()) {
VELOX_DCHECK(status.isUserError());
dayVector->valueAt<int32_t>(row));
if (daysSinceEpoch.hasError()) {
VELOX_DCHECK(daysSinceEpoch.error().isUserError());
return std::nullopt;
}

// Micros has at most 8 digits (2 for seconds + 6 for microseconds),
// thus it's safe to cast micros from int64_t to int32_t.
auto localMicros = util::fromTime(hour, minute, 0, (int32_t)micros);
return util::fromDatetime(daysSinceEpoch, localMicros);
return util::fromDatetime(daysSinceEpoch.value(), localMicros);
}

void setTimestampOrNull(
Expand Down
Loading

0 comments on commit 023be97

Please sign in to comment.