HappenLee commented on code in PR #70485:
URL: https://github.com/apache/doris/pull/70485#discussion_r4583696908
##########
be/src/exprs/function/function_date_or_datetime_computation.h:
##########
@@ -769,21 +769,57 @@ struct DateTimeOp {
}
}
+ static ToType execute(const ValueType0& from, const ValueType1& delta,
+ FunctionContext* context) {
+ if constexpr (std::is_same_v<Transform,
AddYearsImpl<TYPE_TIMESTAMPTZ>> ||
+ std::is_same_v<Transform,
SubtractYearsImpl<TYPE_TIMESTAMPTZ>>) {
+ // Parsed UTC 0000-02-29 can become valid local February 28 in
-08:00.
+ // Reject it before changing years can hide the invalid UTC source.
+ if (!from.is_valid_date()) [[unlikely]] {
+ throw_out_of_bound_date_int(Transform::name, from, delta);
+ }
+ // Use the local date for calendar years. In +08:00, 2024-02-29
00:00
+ // is 2024-02-28 16:00 UTC. A year added in UTC gives March 1
locally.
+ // The DATETIME transform changes February 29 to February 28 when
needed.
+ const auto& timezone = context->state()->timezone_obj();
+ DateV2Value<DateTimeV2ValueType> local;
+ from.convert_utc_to_local(timezone, local);
+ using LocalTransform =
+ std::conditional_t<std::is_same_v<Transform,
AddYearsImpl<TYPE_TIMESTAMPTZ>>,
+ AddYearsImpl<TYPE_DATETIMEV2>,
+ SubtractYearsImpl<TYPE_DATETIMEV2>>;
+ const auto local_result = LocalTransform::execute(local, delta);
+ TimestampTzValue result;
+ // Keep the input offset when the target time occurs twice during
a DST change.
+ // This also keeps years_add(ts, 0) equal to ts in the repeated
hour.
+ result.convert_local_to_utc(timezone, local_result,
from.utc_offset(timezone));
Review Comment:
[P2] Reuse the input timezone lookup when retaining its offset
Each row first calls `from.convert_utc_to_local(timezone, local)` and then
calls `from.utc_offset(timezone)` here. Both helpers reconstruct the same UTC
time point and look it up in the same session zone. In the first helper,
`cctz::convert(utc_tp, timezone)` returns `timezone.lookup(utc_tp).cs`,
discarding the offset that the second helper obtains again.
Please retain both `cs` and `offset` from one input `absolute_lookup`. For
example, the existing UTC-to-local helper could return the offset alongside the
local datetime, and this call could reuse it. This removes the redundant work
from every TIMESTAMPTZ year-arithmetic row while preserving source validation,
fractional seconds, and the preferred-offset behavior for repeated local times.
In a standalone CCTZ microbenchmark of this input-conversion fragment (3
million operations per run, three runs, America/New_York), combining the two
lookups reduced the average from approximately 109 ns/op to 56 ns/op with
identical checksums. This excludes the year transform, output conversion, and
end-to-end SQL execution; it is not a whole-query speedup measurement.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]