Skip to content

Commit 7e73536

Browse files
committed
Refactor
1 parent 651e9b8 commit 7e73536

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

velox/dwio/common/DirectDecoder.h

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -106,26 +106,14 @@ class DirectDecoder : public IntDecoder<isSigned> {
106106
} else if constexpr (std::is_same_v<
107107
typename Visitor::DataType,
108108
int128_t>) {
109-
if (super::numBytes == 12) {
110-
static constexpr int64_t kJulianToUnixEpochDays = 2440588LL;
111-
static constexpr int64_t kSecondsPerDay = 86400LL;
112-
static constexpr int64_t kNanosPerSecond =
113-
Timestamp::kNanosecondsInMillisecond *
114-
Timestamp::kMillisecondsInSecond;
109+
if (super::numBytes == 12 /* INT96 */) {
110+
int128_t encoded = super::template readInt<int128_t>();
111+
int32_t days = encoded & ((1ULL << 32) - 1);
112+
uint64_t nanos = static_cast<uint64_t>(encoded >> 32);
115113

116-
int128_t dat = super::template readInt<int128_t>();
117-
int32_t days = dat & ((1ULL << 32) - 1);
118-
dat = dat >> 32;
119-
uint64_t nanos = static_cast<uint64_t>(dat);
120-
121-
int64_t seconds = (days - kJulianToUnixEpochDays) * kSecondsPerDay;
122-
if (nanos > Timestamp::kMaxNanos) {
123-
seconds += nanos / kNanosPerSecond;
124-
nanos -= (nanos / kNanosPerSecond) * kNanosPerSecond;
125-
}
126-
Timestamp t(seconds, nanos);
127-
auto val = *reinterpret_cast<int128_t*>(&t);
128-
toSkip = visitor.process(val, atEnd);
114+
auto timestamp = Timestamp::fromDaysAndNanos(days, nanos);
115+
toSkip =
116+
visitor.process(*reinterpret_cast<int128_t*>(&timestamp), atEnd);
129117
} else {
130118
toSkip = visitor.process(super::template readInt<int128_t>(), atEnd);
131119
}

velox/dwio/parquet/reader/PageReader.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,7 @@ void PageReader::prepareDictionary(const PageHeader& pageHeader) {
414414
// We start from the end to allow in-place expansion.
415415
auto values = dictionary_.values->asMutable<Timestamp>();
416416
auto parquetValues = dictionary_.values->asMutable<char>();
417-
static constexpr int64_t kJulianToUnixEpochDays = 2440588LL;
418-
static constexpr int64_t kSecondsPerDay = 86400LL;
419-
static constexpr int64_t kNanosPerSecond =
420-
Timestamp::kNanosecondsInMillisecond *
421-
Timestamp::kMillisecondsInSecond;
417+
422418
for (auto i = dictionary_.numValues - 1; i >= 0; --i) {
423419
// Convert the timestamp into seconds and nanos since the Unix epoch,
424420
// 00:00:00.000000 on 1 January 1970.
@@ -432,12 +428,8 @@ void PageReader::prepareDictionary(const PageHeader& pageHeader) {
432428
&days,
433429
parquetValues + i * sizeof(Int96Timestamp) + sizeof(uint64_t),
434430
sizeof(int32_t));
435-
int64_t seconds = (days - kJulianToUnixEpochDays) * kSecondsPerDay;
436-
if (nanos > Timestamp::kMaxNanos) {
437-
seconds += nanos / kNanosPerSecond;
438-
nanos -= (nanos / kNanosPerSecond) * kNanosPerSecond;
439-
}
440-
values[i] = Timestamp(seconds, nanos);
431+
432+
values[i] = Timestamp::fromDaysAndNanos(days, nanos);
441433
}
442434
break;
443435
}

velox/type/Timestamp.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@ struct Timestamp {
8686
VELOX_USER_DCHECK_LE(nanos, kMaxNanos, "Timestamp nanos out of range");
8787
}
8888

89+
static Timestamp fromDaysAndNanos(int32_t days, uint64_t nanos) {
90+
static constexpr int64_t kJulianToUnixEpochDays = 2440588LL;
91+
static constexpr int64_t kSecondsPerDay = 86400LL;
92+
static constexpr int64_t kNanosPerSecond =
93+
Timestamp::kNanosecondsInMillisecond * Timestamp::kMillisecondsInSecond;
94+
95+
int64_t seconds = (days - kJulianToUnixEpochDays) * kSecondsPerDay;
96+
if (nanos > Timestamp::kMaxNanos) {
97+
seconds += nanos / kNanosPerSecond;
98+
nanos -= (nanos / kNanosPerSecond) * kNanosPerSecond;
99+
}
100+
101+
return Timestamp(seconds, nanos);
102+
}
103+
89104
// Returns the current unix timestamp (ms precision).
90105
static Timestamp now();
91106

0 commit comments

Comments
 (0)