Skip to content

[Opt](function) Optimize the performance of FROM_UNIXTIME#57423

Merged
morningman merged 1 commit intoapache:masterfrom
linrrzqqq:opt-from-unixtime
Oct 31, 2025
Merged

[Opt](function) Optimize the performance of FROM_UNIXTIME#57423
morningman merged 1 commit intoapache:masterfrom
linrrzqqq:opt-from-unixtime

Conversation

@linrrzqqq
Copy link
Contributor

What problem does this PR solve?

Issue Number: close #xxx

Related PR: #xxx

Problem Summary:

Before:

mysql> SELECT COUNT(FROM_UNIXTIME(`EventTime`)) FROM hits;
+-----------------------------------+
| COUNT(FROM_UNIXTIME(`EventTime`)) |
+-----------------------------------+
|                          99997497 |
+-----------------------------------+
1 row in set (10.54 sec)

After:

mysql> SELECT COUNT(FROM_UNIXTIME(`EventTime`)) FROM hits;
+-----------------------------------+
| COUNT(FROM_UNIXTIME(`EventTime`)) |
+-----------------------------------+
|                          99997497 |
+-----------------------------------+
1 row in set (5.52 sec)

In from_unixtime, we use cctz::convert to transform an unix timestamp into timezone-specific local time, the calculation formula is as follows: tr.civil_sec + (unix_time-tr.unix_time). This step involves a complex calculation with the addition operator to adjust the civil day accordingly like:

y = 1970; m = 1;
while d > 365:
while d > days_in_century(y):
  d -= days_in_century(y); y += 100;
while d > days_in_4years(y):
  d -= days_in_4years(y); y += 4;
while d > days_in_year(y):
  d -= days_in_year(y);  y += 1;
while d > days_in_month(y,m):
d -= days_in_month(y,m);
if (++m > 12) { y++; m = 1; }
return y, m, d;

Solution:

  1. Look up the transitions to adjust the UTCOffset, which can be efficiently cached.
  2. Look up the unix_days to civil_days cache to obtain the YMD. This cache stores a 200-year mapping starting from 1970-01-01, utilizing minimal memory.

Pseudo code:

int64_t local_unix = unixtime + UTCOffset;
int64_t days = local_unix / 86400;
int64_t seconds_of_day = local_unix % 86400;
civil_day cd = cache[local_unix];
civil_second cs = civil_second(cd)+ seconds_of_day;

Release note

None

Check List (For Author)

  • Test

    • Regression test
    • Unit Test
    • Manual test (add detailed scripts or steps below)
    • No need to test or manual test. Explain why:
      • This is a refactor/code format and no logic has been changed.
      • Previous test can cover this change.
      • No code files have been changed.
      • Other reason
  • Behavior changed:

    • No.
    • Yes.
  • Does this need documentation?

    • No.
    • Yes.

Check List (For Reviewer who merge this PR)

  • Confirm the release note
  • Confirm test cases
  • Confirm document
  • Add branch pick label

@Thearas
Copy link
Contributor

Thearas commented Oct 28, 2025

Thank you for your contribution to Apache Doris.
Don't know what should be done next? See How to process your PR.

Please clearly describe your PR:

  1. What problem was fixed (it's best to include specific error reporting information). How it was fixed.
  2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be.
  3. What features were added. Why was this function added?
  4. Which code was refactored and why was this part of the code refactored?
  5. Which functions were optimized and what is the difference before and after the optimization?

@linrrzqqq
Copy link
Contributor Author

run buildall

@hello-stephen
Copy link
Contributor

BE UT Coverage Report

Increment line coverage 🎉

Increment coverage report
Complete coverage report

Category Coverage
Function Coverage 52.76% (18056/34224)
Line Coverage 37.99% (163725/430950)
Region Coverage 32.38% (124836/385540)
Branch Coverage 33.74% (54568/161729)

@hello-stephen
Copy link
Contributor

BE Regression && UT Coverage Report

Increment line coverage 100% (0/0) 🎉

Increment coverage report
Complete coverage report

Category Coverage
Function Coverage 71.39% (23935/33529)
Line Coverage 57.80% (248967/430751)
Region Coverage 53.05% (207017/390255)
Branch Coverage 54.66% (88824/162502)

@zclllyybb zclllyybb changed the title [Enhancement](from_unixtime) Enhance the performance of FROM_UNIXTIME [Opt](function) Optimize the performance of FROM_UNIXTIME Oct 28, 2025
@github-actions
Copy link
Contributor

PR approved by at least one committer and no changes requested.

@github-actions github-actions bot added approved Indicates a PR has been approved by one committer. reviewed labels Oct 28, 2025
@github-actions
Copy link
Contributor

PR approved by anyone and no changes requested.

@doris-robot
Copy link

ClickBench: Total hot run time: 27.89 s
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/clickbench-tools
ClickBench test result on commit a1984ded322f665f15078d0531ed113101cc0bcd, data reload: false

query1	0.06	0.05	0.05
query2	0.10	0.05	0.06
query3	0.26	0.10	0.08
query4	1.61	0.12	0.12
query5	0.27	0.26	0.25
query6	1.16	0.66	0.66
query7	0.03	0.03	0.03
query8	0.06	0.05	0.04
query9	0.63	0.53	0.52
query10	0.59	0.57	0.58
query11	0.18	0.11	0.11
query12	0.16	0.12	0.12
query13	0.63	0.61	0.61
query14	1.05	1.02	1.03
query15	0.86	0.84	0.84
query16	0.41	0.40	0.39
query17	1.01	1.03	1.07
query18	0.22	0.20	0.19
query19	1.91	1.86	1.82
query20	0.02	0.01	0.01
query21	15.43	0.17	0.12
query22	5.12	0.06	0.05
query23	15.68	0.26	0.10
query24	2.82	0.63	0.81
query25	0.08	0.06	0.05
query26	0.13	0.15	0.13
query27	0.06	0.05	0.05
query28	5.29	1.15	0.92
query29	12.56	4.01	3.33
query30	0.27	0.15	0.11
query31	2.82	0.60	0.39
query32	3.23	0.55	0.48
query33	2.99	3.09	3.03
query34	15.85	5.20	4.54
query35	4.63	4.59	4.61
query36	0.68	0.51	0.50
query37	0.10	0.07	0.07
query38	0.07	0.04	0.04
query39	0.04	0.03	0.04
query40	0.17	0.13	0.15
query41	0.08	0.03	0.03
query42	0.04	0.03	0.02
query43	0.04	0.04	0.04
Total cold run time: 99.4 s
Total hot run time: 27.89 s

@morningman morningman merged commit 0a1905f into apache:master Oct 31, 2025
31 checks passed
morningman pushed a commit that referenced this pull request Oct 31, 2025
…57423 (#57573)

Cherry-picked from #57423

Co-authored-by: linrrarity <linzhenqi@selectdb.com>
@linrrzqqq linrrzqqq deleted the opt-from-unixtime branch November 1, 2025 08:56
yiguolei pushed a commit that referenced this pull request Nov 5, 2025
…57423 (#57574)

Cherry-picked from #57423

Co-authored-by: linrrarity <linzhenqi@selectdb.com>
morningman pushed a commit that referenced this pull request Nov 15, 2025
### What problem does this PR solve?

Issue Number: close #xxx

Related PR: #xxx

Problem Summary:

The construction cost of civil_second in cctz is expensive, details can
be seen: #57423.

In some cases, we do not need to construct the complete civil_time, but
only the UTC offset is required, like:`HOUR(FROM_UNIXTIME(ts))`

This PR supports the lookup_offset function in cctz, which has the same
return type as lookup, but the civil_second is empty, avoiding the
expensive construction cost.

```cpp
return {{}, utc_offset, is_dst, abbreviations[abbr_index]};
```
github-actions bot pushed a commit that referenced this pull request Nov 15, 2025
### What problem does this PR solve?

Issue Number: close #xxx

Related PR: #xxx

Problem Summary:

The construction cost of civil_second in cctz is expensive, details can
be seen: #57423.

In some cases, we do not need to construct the complete civil_time, but
only the UTC offset is required, like:`HOUR(FROM_UNIXTIME(ts))`

This PR supports the lookup_offset function in cctz, which has the same
return type as lookup, but the civil_second is empty, avoiding the
expensive construction cost.

```cpp
return {{}, utc_offset, is_dst, abbreviations[abbr_index]};
```
github-actions bot pushed a commit that referenced this pull request Nov 15, 2025
### What problem does this PR solve?

Issue Number: close #xxx

Related PR: #xxx

Problem Summary:

The construction cost of civil_second in cctz is expensive, details can
be seen: #57423.

In some cases, we do not need to construct the complete civil_time, but
only the UTC offset is required, like:`HOUR(FROM_UNIXTIME(ts))`

This PR supports the lookup_offset function in cctz, which has the same
return type as lookup, but the civil_second is empty, avoiding the
expensive construction cost.

```cpp
return {{}, utc_offset, is_dst, abbreviations[abbr_index]};
```
@yiguolei yiguolei mentioned this pull request Dec 2, 2025
nagisa-kunhah pushed a commit to nagisa-kunhah/doris that referenced this pull request Dec 14, 2025
…e#57999)

### What problem does this PR solve?

Issue Number: close #xxx

Related PR: #xxx

Problem Summary:

The construction cost of civil_second in cctz is expensive, details can
be seen: apache#57423.

In some cases, we do not need to construct the complete civil_time, but
only the UTC offset is required, like:`HOUR(FROM_UNIXTIME(ts))`

This PR supports the lookup_offset function in cctz, which has the same
return type as lookup, but the civil_second is empty, avoiding the
expensive construction cost.

```cpp
return {{}, utc_offset, is_dst, abbreviations[abbr_index]};
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by one committer. dev/3.1.3-merged dev/4.0.2-merged reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants