Coverage Report

Created: 2026-07-14 05:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format_v2/timestamp_statistics.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <cctz/time_zone.h>
21
22
#include <cstdint>
23
24
#include "common/check.h"
25
26
namespace doris::format {
27
28
421
inline int64_t floor_epoch_seconds(int64_t value, int64_t units_per_second) {
29
421
    DORIS_CHECK(units_per_second > 0);
30
421
    auto seconds = value / units_per_second;
31
421
    if (value < 0 && value % units_per_second != 0) {
32
2
        --seconds;
33
2
    }
34
421
    return seconds;
35
421
}
36
37
// UTC instants are ordered, but converting them to civil DATETIMEV2 values is not monotonic when
38
// a timezone moves its clock backward. Metadata min/max converted across such a transition cannot
39
// safely form a ZoneMap: the true civil minimum or maximum may occur inside the UTC interval.
40
inline bool utc_timestamp_range_is_monotonic(int64_t min_seconds, int64_t max_seconds,
41
225
                                             const cctz::time_zone& timezone) {
42
    // These endpoints come from external file metadata. An inverted range is corrupt rather than
43
    // a valid precondition violation, so report it as unusable statistics and let every caller
44
    // take its conservative scan/fallback path instead of terminating the BE.
45
225
    if (min_seconds > max_seconds) {
46
1
        return false;
47
1
    }
48
224
    auto current = cctz::time_point<cctz::seconds>(cctz::seconds(min_seconds));
49
224
    const auto range_end = cctz::time_point<cctz::seconds>(cctz::seconds(max_seconds));
50
224
    cctz::time_zone::civil_transition transition;
51
227
    while (timezone.next_transition(current, &transition)) {
52
14
        const auto transition_time = timezone.lookup(transition.to).trans;
53
14
        if (transition_time > range_end) {
54
6
            return true;
55
6
        }
56
8
        if (transition.to < transition.from) {
57
5
            return false;
58
5
        }
59
        // Move past the transition that was just inspected. Some cctz implementations return the
60
        // same transition again when queried at its exact instant, which would otherwise prevent
61
        // us from seeing a later rollback in the requested range.
62
3
        current = transition_time + cctz::seconds(1);
63
3
    }
64
213
    return true;
65
224
}
66
67
} // namespace doris::format