|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#pragma once |
| 18 | + |
| 19 | +#include "velox/type/Timestamp.h" |
| 20 | + |
| 21 | +namespace facebook::velox::connector::clp::search_lib { |
| 22 | + |
| 23 | +enum class InputTimestampPrecision : uint8_t { |
| 24 | + Seconds, |
| 25 | + Milliseconds, |
| 26 | + Microseconds, |
| 27 | + Nanoseconds |
| 28 | +}; |
| 29 | + |
| 30 | +/// Estimates the precision of an epoch timestamp as seconds, milliseconds, |
| 31 | +/// microseconds, or nanoseconds. |
| 32 | +/// |
| 33 | +/// This heuristic relies on the fact that 1 year of epoch nanoseconds is |
| 34 | +/// approximately 1000 years of epoch microseconds and so on. This heuristic |
| 35 | +/// can be unreliable for timestamps sufficiently close to the epoch, but |
| 36 | +/// should otherwise be accurate for the next 1000 years. |
| 37 | +/// |
| 38 | +/// Note: Future versions of the clp-s archive format will adopt a |
| 39 | +/// nanosecond-precision integer timestamp format (as opposed to the current |
| 40 | +/// format which allows other precisions), at which point we can remove this |
| 41 | +/// heuristic. |
| 42 | +/// |
| 43 | +/// @param timestamp |
| 44 | +/// @return the estimated timestamp precision |
| 45 | +template <typename T> |
| 46 | +auto estimatePrecision(T timestamp) -> InputTimestampPrecision { |
| 47 | + constexpr int64_t kEpochMilliseconds1971{31536000000}; |
| 48 | + constexpr int64_t kEpochMicroseconds1971{31536000000000}; |
| 49 | + constexpr int64_t kEpochNanoseconds1971{31536000000000000}; |
| 50 | + auto absTimestamp = timestamp >= 0 ? timestamp : -timestamp; |
| 51 | + |
| 52 | + if (absTimestamp > kEpochNanoseconds1971) { |
| 53 | + return InputTimestampPrecision::Nanoseconds; |
| 54 | + } else if (absTimestamp > kEpochMicroseconds1971) { |
| 55 | + return InputTimestampPrecision::Microseconds; |
| 56 | + } else if (absTimestamp > kEpochMilliseconds1971) { |
| 57 | + return InputTimestampPrecision::Milliseconds; |
| 58 | + } else { |
| 59 | + return InputTimestampPrecision::Seconds; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/// Converts a double value into a Velox timestamp. |
| 64 | +/// |
| 65 | +/// @param timestamp the input timestamp as a double |
| 66 | +/// @return the corresponding Velox timestamp |
| 67 | +inline auto convertToVeloxTimestamp(double timestamp) -> Timestamp { |
| 68 | + switch (estimatePrecision(timestamp)) { |
| 69 | + case InputTimestampPrecision::Nanoseconds: |
| 70 | + timestamp /= Timestamp::kNanosInSecond; |
| 71 | + break; |
| 72 | + case InputTimestampPrecision::Microseconds: |
| 73 | + timestamp /= Timestamp::kMicrosecondsInSecond; |
| 74 | + break; |
| 75 | + case InputTimestampPrecision::Milliseconds: |
| 76 | + timestamp /= Timestamp::kMillisecondsInSecond; |
| 77 | + break; |
| 78 | + case InputTimestampPrecision::Seconds: |
| 79 | + break; |
| 80 | + } |
| 81 | + double seconds{std::floor(timestamp)}; |
| 82 | + double nanoseconds{(timestamp - seconds) * Timestamp::kNanosInSecond}; |
| 83 | + return Timestamp( |
| 84 | + static_cast<int64_t>(seconds), static_cast<uint64_t>(nanoseconds)); |
| 85 | +} |
| 86 | + |
| 87 | +/// Converts an integer value into a Velox timestamp. |
| 88 | +/// |
| 89 | +/// @param timestamp the input timestamp as an integer |
| 90 | +/// @return the corresponding Velox timestamp |
| 91 | +inline auto convertToVeloxTimestamp(int64_t timestamp) -> Timestamp { |
| 92 | + int64_t precisionDifference{Timestamp::kNanosInSecond}; |
| 93 | + switch (estimatePrecision(timestamp)) { |
| 94 | + case InputTimestampPrecision::Nanoseconds: |
| 95 | + break; |
| 96 | + case InputTimestampPrecision::Microseconds: |
| 97 | + precisionDifference = |
| 98 | + Timestamp::kNanosInSecond / Timestamp::kNanosecondsInMicrosecond; |
| 99 | + break; |
| 100 | + case InputTimestampPrecision::Milliseconds: |
| 101 | + precisionDifference = |
| 102 | + Timestamp::kNanosInSecond / Timestamp::kNanosecondsInMillisecond; |
| 103 | + break; |
| 104 | + case InputTimestampPrecision::Seconds: |
| 105 | + precisionDifference = |
| 106 | + Timestamp::kNanosInSecond / Timestamp::kNanosInSecond; |
| 107 | + break; |
| 108 | + } |
| 109 | + int64_t seconds{timestamp / precisionDifference}; |
| 110 | + int64_t nanoseconds{ |
| 111 | + (timestamp % precisionDifference) * |
| 112 | + (Timestamp::kNanosInSecond / precisionDifference)}; |
| 113 | + if (nanoseconds < 0) { |
| 114 | + seconds -= 1; |
| 115 | + nanoseconds += Timestamp::kNanosInSecond; |
| 116 | + } |
| 117 | + return Timestamp(seconds, static_cast<uint64_t>(nanoseconds)); |
| 118 | +} |
| 119 | + |
| 120 | +} // namespace facebook::velox::connector::clp::search_lib |
0 commit comments