-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathmatcher.rs
More file actions
108 lines (97 loc) · 3.67 KB
/
matcher.rs
File metadata and controls
108 lines (97 loc) · 3.67 KB
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use std::fmt::Display;
use std::sync::Arc;
use vortex_error::VortexResult;
use crate::dtype::extension::ExtDTypeRef;
use crate::dtype::extension::ExtVTable;
use crate::extension::datetime::Date;
use crate::extension::datetime::Time;
use crate::extension::datetime::TimeUnit;
use crate::extension::datetime::Timestamp;
use crate::matcher::Matcher;
use crate::matcher::MatcherType;
/// Matcher for temporal extension data types.
pub struct AnyTemporal;
impl MatcherType<ExtDTypeRef> for AnyTemporal {
type Match<'a> = TemporalMetadata<'a>;
}
impl Matcher<ExtDTypeRef> for AnyTemporal {
fn try_match<'a>(item: &'a ExtDTypeRef) -> Option<Self::Match<'a>> {
if let Some(opts) = item.metadata_opt::<Timestamp>() {
return Some(TemporalMetadata::Timestamp(&opts.unit, &opts.tz));
}
if let Some(opts) = item.metadata_opt::<Date>() {
return Some(TemporalMetadata::Date(opts));
}
if let Some(opts) = item.metadata_opt::<Time>() {
return Some(TemporalMetadata::Time(opts));
}
None
}
}
/// Metadata for temporal extension data types.
#[derive(Debug, PartialEq, Eq)]
pub enum TemporalMetadata<'a> {
/// Metadata for Timestamp dtypes, a tuple of time unit and optional timezone.
Timestamp(&'a TimeUnit, &'a Option<Arc<str>>),
/// Metadata for Date dtypes
Date(&'a <Date as ExtVTable>::Metadata),
/// Metadata for Time dtypes
Time(&'a <Time as ExtVTable>::Metadata),
}
// TODO(ngates): remove this logic in favor of having an ExtScalarVTable in vortex_array::scalar.
// Currently this is used largely to implement scalar display hacks.
impl TemporalMetadata<'_> {
/// Get the time unit of the temporal dtype.
pub fn time_unit(&self) -> TimeUnit {
match self {
TemporalMetadata::Time(unit) => **unit,
TemporalMetadata::Date(unit) => **unit,
TemporalMetadata::Timestamp(unit, _tz) => **unit,
}
}
/// Convert a timestamp value to a Jiff value.
pub fn to_jiff(&self, v: i64) -> VortexResult<TemporalJiff> {
match self {
TemporalMetadata::Time(unit) => Ok(TemporalJiff::Time(
jiff::civil::Time::MIN.checked_add(unit.to_jiff_span(v)?)?,
)),
TemporalMetadata::Date(unit) => Ok(TemporalJiff::Date(
jiff::civil::Date::new(1970, 1, 1)?.checked_add(unit.to_jiff_span(v)?)?,
)),
TemporalMetadata::Timestamp(unit, tz) => match tz {
None => Ok(TemporalJiff::Unzoned(
jiff::civil::DateTime::new(1970, 1, 1, 0, 0, 0, 0)?
.checked_add(unit.to_jiff_span(v)?)?,
)),
Some(tz) => Ok(TemporalJiff::Zoned(
jiff::Timestamp::UNIX_EPOCH
.checked_add(unit.to_jiff_span(v)?)?
.in_tz(tz.as_ref())?,
)),
},
}
}
}
/// A Jiff representation of a temporal value.
pub enum TemporalJiff {
/// A time value.
Time(jiff::civil::Time),
/// A date value.
Date(jiff::civil::Date),
/// A zone-naive timestamp value.
Unzoned(jiff::civil::DateTime),
/// A zoned timestamp value.
Zoned(jiff::Zoned),
}
impl Display for TemporalJiff {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TemporalJiff::Time(t) => write!(f, "{t}"),
TemporalJiff::Date(d) => write!(f, "{d}"),
TemporalJiff::Unzoned(dt) => write!(f, "{dt}"),
TemporalJiff::Zoned(z) => write!(f, "{z}"),
}
}
}