Skip to content

Commit e2695d2

Browse files
committed
feat(expr): add timestamp timezone replacement
Signed-off-by: Daniel King <dan@spiraldb.com>
1 parent 0852739 commit e2695d2

11 files changed

Lines changed: 485 additions & 0 deletions

File tree

‎vortex-array/proto/expr.proto‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,9 @@ message SelectOpts {
129129
message CaseWhenOpts {
130130
uint32 num_children = 1;
131131
}
132+
133+
// Options for `vortex.replace_time_zone`. Ambiguity policy is an expression child.
134+
message ReplaceTimeZoneOpts {
135+
optional string time_zone = 1;
136+
bool null_on_non_existent = 2;
137+
}

‎vortex-array/src/expr/exprs.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ use crate::scalar_fn::fns::operators::CompareOperator;
5252
use crate::scalar_fn::fns::operators::Operator;
5353
use crate::scalar_fn::fns::pack::Pack;
5454
use crate::scalar_fn::fns::pack::PackOptions;
55+
use crate::scalar_fn::fns::replace_time_zone::ReplaceTimeZone;
56+
use crate::scalar_fn::fns::replace_time_zone::ReplaceTimeZoneOptions;
5557
use crate::scalar_fn::fns::select::FieldSelection;
5658
use crate::scalar_fn::fns::select::Select;
5759
use crate::scalar_fn::fns::variant_get::VariantGet;
@@ -1282,6 +1284,29 @@ pub fn bound_list_sum_opts(
12821284
.vortex_expect("list-sum expressions require a numeric list child")
12831285
}
12841286

1287+
/// Reinterpret timestamp wall times in `options.time_zone`, preserving the time unit.
1288+
///
1289+
/// `ambiguous` is a UTF-8 expression containing `raise`, `earliest`, `latest`, or `null`.
1290+
/// Removing the timezone preserves wall time in a timezone-naive timestamp.
1291+
pub fn replace_time_zone(
1292+
input: Expression,
1293+
ambiguous: Expression,
1294+
options: ReplaceTimeZoneOptions,
1295+
) -> Expression {
1296+
ReplaceTimeZone.new_expr(options, [input, ambiguous])
1297+
}
1298+
1299+
/// Create a bound timezone replacement expression.
1300+
pub fn bound_replace_time_zone(
1301+
input: BoundExpression,
1302+
ambiguous: BoundExpression,
1303+
options: ReplaceTimeZoneOptions,
1304+
) -> BoundExpression {
1305+
ReplaceTimeZone
1306+
.try_new_bound_expr(options, [input, ambiguous])
1307+
.vortex_expect("timezone replacement requires a timestamp and a UTF-8 ambiguity policy")
1308+
}
1309+
12851310
/// Constructors for expressions whose children have already been bound and type-checked.
12861311
///
12871312
/// These mirror the constructors in [`crate::expr`] and panic when the supplied children do not
@@ -1330,6 +1355,7 @@ pub mod bound {
13301355
pub use super::bound_or as or;
13311356
pub use super::bound_or_collect as or_collect;
13321357
pub use super::bound_pack as pack;
1358+
pub use super::bound_replace_time_zone as replace_time_zone;
13331359
pub use super::bound_root as root;
13341360
pub use super::bound_select as select;
13351361
pub use super::bound_select_exclude as select_exclude;

‎vortex-array/src/expr/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ pub use exprs::not_like;
104104
pub use exprs::or;
105105
pub use exprs::or_collect;
106106
pub use exprs::pack;
107+
pub use exprs::replace_time_zone;
107108
pub use exprs::root;
108109
pub use exprs::select;
109110
pub use exprs::select_exclude;

‎vortex-array/src/proto/generated/vortex.expr.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,11 @@ pub struct CaseWhenOpts {
217217
#[prost(uint32, tag = "1")]
218218
pub num_children: u32,
219219
}
220+
/// Options for `vortex.replace_time_zone`. Ambiguity policy is an expression child.
221+
#[derive(Clone, PartialEq, Eq, Hash, ::prost::Message)]
222+
pub struct ReplaceTimeZoneOpts {
223+
#[prost(string, optional, tag = "1")]
224+
pub time_zone: ::core::option::Option<::prost::alloc::string::String>,
225+
#[prost(bool, tag = "2")]
226+
pub null_on_non_existent: bool,
227+
}

‎vortex-array/src/scalar_fn/fns/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod merge;
2323
pub mod not;
2424
pub mod operators;
2525
pub mod pack;
26+
pub mod replace_time_zone;
2627
pub mod select;
2728
pub mod stat;
2829
pub mod variant_get;

0 commit comments

Comments
 (0)