-
Notifications
You must be signed in to change notification settings - Fork 5
/
timestamp.rs
314 lines (284 loc) · 11.3 KB
/
timestamp.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use std::sync::Arc;
use crate::{
array_decoder::ArrowDataType,
column::Column,
encoding::{
integer::{get_rle_reader, get_unsigned_rle_reader},
timestamp::{TimestampDecoder, TimestampNanosecondAsDecimalDecoder},
PrimitiveValueDecoder,
},
error::{MismatchedSchemaSnafu, Result},
proto::stream::Kind,
stripe::Stripe,
};
use arrow::datatypes::{
ArrowTimestampType, Decimal128Type, DecimalType, TimeUnit, TimestampMicrosecondType,
TimestampMillisecondType, TimestampNanosecondType, TimestampSecondType,
};
use arrow::{array::ArrayRef, buffer::NullBuffer};
use chrono::offset::TimeZone;
use chrono::TimeDelta;
use chrono_tz::{Tz, UTC};
use super::{
decimal::DecimalArrayDecoder, ArrayBatchDecoder, PresentDecoder, PrimitiveArrayDecoder,
};
use crate::error::UnsupportedTypeVariantSnafu;
const NANOSECONDS_IN_SECOND: i128 = 1_000_000_000;
const NANOSECOND_DIGITS: i8 = 9;
/// Seconds from ORC epoch of 1 January 2015, which serves as the 0
/// point for all timestamp values, to the UNIX epoch of 1 January 1970.
const ORC_EPOCH_UTC_SECONDS_SINCE_UNIX_EPOCH: i64 = 1_420_070_400;
fn get_inner_timestamp_decoder<T: ArrowTimestampType + Send>(
column: &Column,
stripe: &Stripe,
seconds_since_unix_epoch: i64,
) -> Result<PrimitiveArrayDecoder<T>> {
let data = stripe.stream_map().get(column, Kind::Data);
let data = get_rle_reader(column, data)?;
let secondary = stripe.stream_map().get(column, Kind::Secondary);
let secondary = get_unsigned_rle_reader(column, secondary);
let present = PresentDecoder::from_stripe(stripe, column);
let iter = Box::new(TimestampDecoder::<T>::new(
seconds_since_unix_epoch,
data,
secondary,
));
Ok(PrimitiveArrayDecoder::<T>::new(iter, present))
}
fn get_timestamp_decoder<T: ArrowTimestampType + Send>(
column: &Column,
stripe: &Stripe,
seconds_since_unix_epoch: i64,
) -> Result<Box<dyn ArrayBatchDecoder>> {
let inner = get_inner_timestamp_decoder::<T>(column, stripe, seconds_since_unix_epoch)?;
match stripe.writer_tz() {
Some(writer_tz) => Ok(Box::new(TimestampOffsetArrayDecoder { inner, writer_tz })),
None => Ok(Box::new(inner)),
}
}
fn get_timestamp_instant_decoder<T: ArrowTimestampType + Send>(
column: &Column,
stripe: &Stripe,
) -> Result<Box<dyn ArrayBatchDecoder>> {
// TIMESTAMP_INSTANT is encoded as UTC so we don't check writer timezone in stripe
let inner =
get_inner_timestamp_decoder::<T>(column, stripe, ORC_EPOCH_UTC_SECONDS_SINCE_UNIX_EPOCH)?;
Ok(Box::new(TimestampInstantArrayDecoder(inner)))
}
fn decimal128_decoder(
column: &Column,
stripe: &Stripe,
seconds_since_unix_epoch: i64,
writer_tz: Option<Tz>,
) -> Result<DecimalArrayDecoder> {
let data = stripe.stream_map().get(column, Kind::Data);
let data = get_rle_reader(column, data)?;
let secondary = stripe.stream_map().get(column, Kind::Secondary);
let secondary = get_rle_reader(column, secondary)?;
let present = PresentDecoder::from_stripe(stripe, column);
let iter = TimestampNanosecondAsDecimalDecoder::new(seconds_since_unix_epoch, data, secondary);
let iter: Box<dyn PrimitiveValueDecoder<i128> + Send> = match writer_tz {
Some(UTC) | None => Box::new(iter),
Some(writer_tz) => Box::new(TimestampNanosecondAsDecimalWithTzDecoder(iter, writer_tz)),
};
Ok(DecimalArrayDecoder::new(
Decimal128Type::MAX_PRECISION,
NANOSECOND_DIGITS,
iter,
present,
))
}
/// Decodes a TIMESTAMP column stripe into batches of Timestamp{Nano,Micro,Milli,}secondArrays
/// with no timezone. Will convert timestamps from writer timezone to UTC if a writer timezone
/// is specified for the stripe.
pub fn new_timestamp_decoder(
column: &Column,
field_type: ArrowDataType,
stripe: &Stripe,
) -> Result<Box<dyn ArrayBatchDecoder>> {
let seconds_since_unix_epoch = match stripe.writer_tz() {
Some(writer_tz) => {
// If writer timezone exists then we must take the ORC epoch according
// to that timezone, and find seconds since UTC UNIX epoch for the base.
writer_tz
.with_ymd_and_hms(2015, 1, 1, 0, 0, 0)
.unwrap()
.timestamp()
}
None => {
// No writer timezone, we can assume UTC, so we can use known fixed value
// for the base offset.
ORC_EPOCH_UTC_SECONDS_SINCE_UNIX_EPOCH
}
};
match field_type {
ArrowDataType::Timestamp(TimeUnit::Second, None) => {
get_timestamp_decoder::<TimestampSecondType>(column, stripe, seconds_since_unix_epoch)
}
ArrowDataType::Timestamp(TimeUnit::Millisecond, None) => {
get_timestamp_decoder::<TimestampMillisecondType>(
column,
stripe,
seconds_since_unix_epoch,
)
}
ArrowDataType::Timestamp(TimeUnit::Microsecond, None) => {
get_timestamp_decoder::<TimestampMicrosecondType>(
column,
stripe,
seconds_since_unix_epoch,
)
}
ArrowDataType::Timestamp(TimeUnit::Nanosecond, None) => {
get_timestamp_decoder::<TimestampNanosecondType>(
column,
stripe,
seconds_since_unix_epoch,
)
}
ArrowDataType::Decimal128(Decimal128Type::MAX_PRECISION, NANOSECOND_DIGITS) => {
Ok(Box::new(decimal128_decoder(
column,
stripe,
seconds_since_unix_epoch,
stripe.writer_tz(),
)?))
}
_ => MismatchedSchemaSnafu {
orc_type: column.data_type().clone(),
arrow_type: field_type,
}
.fail(),
}
}
/// Decodes a TIMESTAMP_INSTANT column stripe into batches of
/// Timestamp{Nano,Micro,Milli,}secondArrays with UTC timezone.
pub fn new_timestamp_instant_decoder(
column: &Column,
field_type: ArrowDataType,
stripe: &Stripe,
) -> Result<Box<dyn ArrayBatchDecoder>> {
match field_type {
ArrowDataType::Timestamp(TimeUnit::Second, Some(tz)) if tz.as_ref() == "UTC" => {
get_timestamp_instant_decoder::<TimestampSecondType>(column, stripe)
}
ArrowDataType::Timestamp(TimeUnit::Millisecond, Some(tz)) if tz.as_ref() == "UTC" => {
get_timestamp_instant_decoder::<TimestampMillisecondType>(column, stripe)
}
ArrowDataType::Timestamp(TimeUnit::Microsecond, Some(tz)) if tz.as_ref() == "UTC" => {
get_timestamp_instant_decoder::<TimestampMicrosecondType>(column, stripe)
}
ArrowDataType::Timestamp(TimeUnit::Nanosecond, Some(tz)) if tz.as_ref() == "UTC" => {
get_timestamp_instant_decoder::<TimestampNanosecondType>(column, stripe)
}
ArrowDataType::Timestamp(_, Some(_)) => UnsupportedTypeVariantSnafu {
msg: "Non-UTC Arrow timestamps",
}
.fail(),
ArrowDataType::Decimal128(Decimal128Type::MAX_PRECISION, NANOSECOND_DIGITS) => {
Ok(Box::new(decimal128_decoder(
column,
stripe,
ORC_EPOCH_UTC_SECONDS_SINCE_UNIX_EPOCH,
None,
)?))
}
_ => MismatchedSchemaSnafu {
orc_type: column.data_type().clone(),
arrow_type: field_type,
}
.fail()?,
}
}
/// Wrapper around PrimitiveArrayDecoder to decode timestamps which are encoded in
/// timezone of the writer to their UTC value.
struct TimestampOffsetArrayDecoder<T: ArrowTimestampType> {
inner: PrimitiveArrayDecoder<T>,
writer_tz: chrono_tz::Tz,
}
impl<T: ArrowTimestampType> ArrayBatchDecoder for TimestampOffsetArrayDecoder<T> {
fn next_batch(
&mut self,
batch_size: usize,
parent_present: Option<&NullBuffer>,
) -> Result<ArrayRef> {
let array = self
.inner
.next_primitive_batch(batch_size, parent_present)?;
let convert_timezone = |ts| {
// Convert from writer timezone to reader timezone (which we default to UTC)
// TODO: more efficient way of doing this?
self.writer_tz
.timestamp_nanos(ts)
.naive_local()
.and_utc()
.timestamp_nanos_opt()
};
let array = array
// first try to convert all non-nullable batches to non-nullable batches
.try_unary::<_, T, _>(|ts| convert_timezone(ts).ok_or(()))
// in the rare case one of the values was out of the timeunit's range (eg. see
// <https://docs.rs/chrono/latest/chrono/struct.DateTime.html#method.timestamp_nanos_opt>),
// for nanoseconds), try again by allowing a nullable batch as output
.unwrap_or_else(|()| array.unary_opt::<_, T>(convert_timezone));
let array = Arc::new(array) as ArrayRef;
Ok(array)
}
}
/// Wrapper around PrimitiveArrayDecoder to allow specifying the timezone of the output
/// timestamp array as UTC.
struct TimestampInstantArrayDecoder<T: ArrowTimestampType>(PrimitiveArrayDecoder<T>);
impl<T: ArrowTimestampType> ArrayBatchDecoder for TimestampInstantArrayDecoder<T> {
fn next_batch(
&mut self,
batch_size: usize,
parent_present: Option<&NullBuffer>,
) -> Result<ArrayRef> {
let array = self
.0
.next_primitive_batch(batch_size, parent_present)?
.with_timezone("UTC");
let array = Arc::new(array) as ArrayRef;
Ok(array)
}
}
struct TimestampNanosecondAsDecimalWithTzDecoder(TimestampNanosecondAsDecimalDecoder, Tz);
impl TimestampNanosecondAsDecimalWithTzDecoder {
fn next_inner(&self, ts: i128) -> i128 {
let seconds = ts.div_euclid(NANOSECONDS_IN_SECOND);
let nanoseconds = ts.rem_euclid(NANOSECONDS_IN_SECOND);
// The addition may panic, because chrono stores dates in an i32,
// which can be overflowed with an i64 of seconds.
let dt = (self.1.timestamp_nanos(0)
+ TimeDelta::new(seconds as i64, nanoseconds as u32)
.expect("TimeDelta duration out of bound"))
.naive_local()
.and_utc();
(dt.timestamp() as i128) * NANOSECONDS_IN_SECOND + (dt.timestamp_subsec_nanos() as i128)
}
}
impl PrimitiveValueDecoder<i128> for TimestampNanosecondAsDecimalWithTzDecoder {
fn decode(&mut self, out: &mut [i128]) -> Result<()> {
self.0.decode(out)?;
for x in out.iter_mut() {
*x = self.next_inner(*x);
}
Ok(())
}
}