-
Notifications
You must be signed in to change notification settings - Fork 976
/
datetime.rs
449 lines (388 loc) · 15.1 KB
/
datetime.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// 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 crate::expressions::delta::shift_months;
use crate::PhysicalExpr;
use arrow::datatypes::{DataType, Schema};
use arrow::record_batch::RecordBatch;
use chrono::{Duration, NaiveDate};
use datafusion_common::Result;
use datafusion_common::{DataFusionError, ScalarValue};
use datafusion_expr::{ColumnarValue, Operator};
use std::any::Any;
use std::fmt::{Display, Formatter};
use std::ops::{Add, Sub};
use std::sync::Arc;
/// Perform DATE +/ INTERVAL math
#[derive(Debug)]
pub struct DateIntervalExpr {
lhs: Arc<dyn PhysicalExpr>,
op: Operator,
rhs: Arc<dyn PhysicalExpr>,
}
impl DateIntervalExpr {
/// Create a new instance of DateIntervalExpr
pub fn try_new(
lhs: Arc<dyn PhysicalExpr>,
op: Operator,
rhs: Arc<dyn PhysicalExpr>,
input_schema: &Schema,
) -> Result<Self> {
match lhs.data_type(input_schema)? {
DataType::Date32 | DataType::Date64 => match rhs.data_type(input_schema)? {
DataType::Interval(_) => match &op {
Operator::Plus | Operator::Minus => Ok(Self { lhs, op, rhs }),
_ => Err(DataFusionError::Execution(format!(
"Invalid operator '{}' for DateIntervalExpr",
op
))),
},
other => Err(DataFusionError::Execution(format!(
"Invalid rhs type '{}' for DateIntervalExpr",
other
))),
},
other => Err(DataFusionError::Execution(format!(
"Invalid lhs type '{}' for DateIntervalExpr",
other
))),
}
}
}
impl Display for DateIntervalExpr {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{} {} {}", self.lhs, self.op, self.rhs)
}
}
impl PhysicalExpr for DateIntervalExpr {
fn as_any(&self) -> &dyn Any {
self
}
fn data_type(&self, input_schema: &Schema) -> Result<DataType> {
self.lhs.data_type(input_schema)
}
fn nullable(&self, input_schema: &Schema) -> Result<bool> {
self.lhs.nullable(input_schema)
}
fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
let dates = self.lhs.evaluate(batch)?;
let intervals = self.rhs.evaluate(batch)?;
// Unwrap days since epoch
let operand = match dates {
ColumnarValue::Scalar(scalar) => scalar,
_ => Err(DataFusionError::Execution(
"Columnar execution is not yet supported for DateIntervalExpr"
.to_string(),
))?,
};
// Convert to NaiveDate
let epoch = NaiveDate::from_ymd(1970, 1, 1);
let prior = match operand {
ScalarValue::Date32(Some(d)) => epoch.add(Duration::days(d as i64)),
ScalarValue::Date64(Some(ms)) => epoch.add(Duration::milliseconds(ms)),
_ => Err(DataFusionError::Execution(format!(
"Invalid lhs type for DateIntervalExpr: {:?}",
operand
)))?,
};
// Unwrap interval to add
let scalar = match &intervals {
ColumnarValue::Scalar(interval) => interval,
_ => Err(DataFusionError::Execution(
"Columnar execution is not yet supported for DateIntervalExpr"
.to_string(),
))?,
};
// Invert sign for subtraction
let sign = match &self.op {
Operator::Plus => 1,
Operator::Minus => -1,
_ => {
// this should be unreachable because we check the operators in `try_new`
Err(DataFusionError::Execution(
"Invalid operator for DateIntervalExpr".to_string(),
))?
}
};
// Do math
let posterior = match scalar {
ScalarValue::IntervalDayTime(Some(i)) => add_day_time(prior, *i, sign),
ScalarValue::IntervalYearMonth(Some(i)) => shift_months(prior, *i * sign),
ScalarValue::IntervalMonthDayNano(Some(i)) => add_m_d_nano(prior, *i, sign),
other => Err(DataFusionError::Execution(format!(
"DateIntervalExpr does not support non-interval type {:?}",
other
)))?,
};
// convert back
let res = match operand {
ScalarValue::Date32(Some(_)) => {
let days = posterior.sub(epoch).num_days() as i32;
ColumnarValue::Scalar(ScalarValue::Date32(Some(days)))
}
ScalarValue::Date64(Some(_)) => {
let ms = posterior.sub(epoch).num_milliseconds();
ColumnarValue::Scalar(ScalarValue::Date64(Some(ms)))
}
_ => Err(DataFusionError::Execution(format!(
"Invalid lhs type for DateIntervalExpr: {}",
scalar
)))?,
};
Ok(res)
}
}
// Can remove once https://github.com/apache/arrow-rs/pull/2031 is released
fn add_m_d_nano(prior: NaiveDate, interval: i128, sign: i32) -> NaiveDate {
let interval = interval as u128;
let nanos = (interval >> 64) as i64 * sign as i64;
let days = (interval >> 32) as i32 * sign;
let months = interval as i32 * sign;
let a = shift_months(prior, months);
let b = a.add(Duration::days(days as i64));
b.add(Duration::nanoseconds(nanos))
}
// Can remove once https://github.com/apache/arrow-rs/pull/2031 is released
fn add_day_time(prior: NaiveDate, interval: i64, sign: i32) -> NaiveDate {
let interval = interval as u64;
let days = (interval >> 32) as i32 * sign;
let ms = interval as i32 * sign;
let intermediate = prior.add(Duration::days(days as i64));
intermediate.add(Duration::milliseconds(ms as i64))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::create_physical_expr;
use crate::execution_props::ExecutionProps;
use arrow::array::{ArrayRef, Date32Builder};
use arrow::datatypes::*;
use datafusion_common::{Result, ToDFSchema};
use datafusion_expr::Expr;
#[test]
fn add_11_months() {
let prior = NaiveDate::from_ymd(2000, 1, 1);
let actual = shift_months(prior, 11);
assert_eq!(format!("{:?}", actual).as_str(), "2000-12-01");
}
#[test]
fn add_12_months() {
let prior = NaiveDate::from_ymd(2000, 1, 1);
let actual = shift_months(prior, 12);
assert_eq!(format!("{:?}", actual).as_str(), "2001-01-01");
}
#[test]
fn add_13_months() {
let prior = NaiveDate::from_ymd(2000, 1, 1);
let actual = shift_months(prior, 13);
assert_eq!(format!("{:?}", actual).as_str(), "2001-02-01");
}
#[test]
fn sub_11_months() {
let prior = NaiveDate::from_ymd(2000, 1, 1);
let actual = shift_months(prior, -11);
assert_eq!(format!("{:?}", actual).as_str(), "1999-02-01");
}
#[test]
fn sub_12_months() {
let prior = NaiveDate::from_ymd(2000, 1, 1);
let actual = shift_months(prior, -12);
assert_eq!(format!("{:?}", actual).as_str(), "1999-01-01");
}
#[test]
fn sub_13_months() {
let prior = NaiveDate::from_ymd(2000, 1, 1);
let actual = shift_months(prior, -13);
assert_eq!(format!("{:?}", actual).as_str(), "1998-12-01");
}
#[test]
fn add_32_day_time() -> Result<()> {
// setup
let dt = Expr::Literal(ScalarValue::Date32(Some(0)));
let op = Operator::Plus;
let interval = create_day_time(1, 0);
let interval = Expr::Literal(ScalarValue::IntervalDayTime(Some(interval)));
// exercise
let res = exercise(&dt, op, &interval)?;
// assert
match res {
ColumnarValue::Scalar(ScalarValue::Date32(Some(d))) => {
let epoch = NaiveDate::from_ymd(1970, 1, 1);
let res = epoch.add(Duration::days(d as i64));
assert_eq!(format!("{:?}", res).as_str(), "1970-01-02");
}
_ => Err(DataFusionError::NotImplemented(
"Unexpected result!".to_string(),
))?,
}
Ok(())
}
#[test]
fn sub_32_year_month() -> Result<()> {
// setup
let dt = Expr::Literal(ScalarValue::Date32(Some(0)));
let op = Operator::Minus;
let interval = Expr::Literal(ScalarValue::IntervalYearMonth(Some(13)));
// exercise
let res = exercise(&dt, op, &interval)?;
// assert
match res {
ColumnarValue::Scalar(ScalarValue::Date32(Some(d))) => {
let epoch = NaiveDate::from_ymd(1970, 1, 1);
let res = epoch.add(Duration::days(d as i64));
assert_eq!(format!("{:?}", res).as_str(), "1968-12-01");
}
_ => Err(DataFusionError::NotImplemented(
"Unexpected result!".to_string(),
))?,
}
Ok(())
}
#[test]
fn add_64_day_time() -> Result<()> {
// setup
let dt = Expr::Literal(ScalarValue::Date64(Some(0)));
let op = Operator::Plus;
let interval = create_day_time(-15, -24 * 60 * 60 * 1000);
let interval = Expr::Literal(ScalarValue::IntervalDayTime(Some(interval)));
// exercise
let res = exercise(&dt, op, &interval)?;
// assert
match res {
ColumnarValue::Scalar(ScalarValue::Date64(Some(d))) => {
let epoch = NaiveDate::from_ymd(1970, 1, 1);
let res = epoch.add(Duration::milliseconds(d as i64));
assert_eq!(format!("{:?}", res).as_str(), "1969-12-16");
}
_ => Err(DataFusionError::NotImplemented(
"Unexpected result!".to_string(),
))?,
}
Ok(())
}
#[test]
fn add_32_year_month() -> Result<()> {
// setup
let dt = Expr::Literal(ScalarValue::Date32(Some(0)));
let op = Operator::Plus;
let interval = Expr::Literal(ScalarValue::IntervalYearMonth(Some(1)));
// exercise
let res = exercise(&dt, op, &interval)?;
// assert
match res {
ColumnarValue::Scalar(ScalarValue::Date32(Some(d))) => {
let epoch = NaiveDate::from_ymd(1970, 1, 1);
let res = epoch.add(Duration::days(d as i64));
assert_eq!(format!("{:?}", res).as_str(), "1970-02-01");
}
_ => Err(DataFusionError::NotImplemented(
"Unexpected result!".to_string(),
))?,
}
Ok(())
}
#[test]
fn add_32_month_day_nano() -> Result<()> {
// setup
let dt = Expr::Literal(ScalarValue::Date32(Some(0)));
let op = Operator::Plus;
let interval = create_month_day_nano(-12, -15, -42);
let interval = Expr::Literal(ScalarValue::IntervalMonthDayNano(Some(interval)));
// exercise
let res = exercise(&dt, op, &interval)?;
// assert
match res {
ColumnarValue::Scalar(ScalarValue::Date32(Some(d))) => {
let epoch = NaiveDate::from_ymd(1970, 1, 1);
let res = epoch.add(Duration::days(d as i64));
assert_eq!(format!("{:?}", res).as_str(), "1968-12-17");
}
_ => Err(DataFusionError::NotImplemented(
"Unexpected result!".to_string(),
))?,
}
Ok(())
}
#[test]
fn invalid_interval() -> Result<()> {
// setup
let dt = Expr::Literal(ScalarValue::Date32(Some(0)));
let op = Operator::Plus;
let interval = Expr::Literal(ScalarValue::Null);
// exercise
let res = exercise(&dt, op, &interval);
assert!(res.is_err(), "Can't add a NULL interval");
Ok(())
}
#[test]
fn invalid_date() -> Result<()> {
// setup
let dt = Expr::Literal(ScalarValue::Null);
let op = Operator::Plus;
let interval = Expr::Literal(ScalarValue::IntervalMonthDayNano(Some(0)));
// exercise
let res = exercise(&dt, op, &interval);
assert!(res.is_err(), "Can't add to NULL date");
Ok(())
}
#[test]
fn invalid_op() -> Result<()> {
// setup
let dt = Expr::Literal(ScalarValue::Date32(Some(0)));
let op = Operator::Eq;
let interval = Expr::Literal(ScalarValue::IntervalMonthDayNano(Some(0)));
// exercise
let res = exercise(&dt, op, &interval);
assert!(res.is_err(), "Can't add dates with == operator");
Ok(())
}
fn exercise(dt: &Expr, op: Operator, interval: &Expr) -> Result<ColumnarValue> {
let mut builder = Date32Builder::new(1);
builder.append_value(0);
let a: ArrayRef = Arc::new(builder.finish());
let schema = Schema::new(vec![Field::new("a", DataType::Date32, false)]);
let batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![a])?;
let dfs = schema.clone().to_dfschema()?;
let props = ExecutionProps::new();
let lhs = create_physical_expr(dt, &dfs, &schema, &props)?;
let rhs = create_physical_expr(interval, &dfs, &schema, &props)?;
let cut = DateIntervalExpr::try_new(lhs, op, rhs, &schema)?;
let res = cut.evaluate(&batch)?;
Ok(res)
}
// Can remove once https://github.com/apache/arrow-rs/pull/2031 is released
/// Creates an IntervalDayTime given its constituent components
///
/// https://github.com/apache/arrow-rs/blob/e59b023480437f67e84ba2f827b58f78fd44c3a1/integration-testing/src/lib.rs#L222
fn create_day_time(days: i32, millis: i32) -> i64 {
let m = millis as u64 & u32::MAX as u64;
let d = (days as u64 & u32::MAX as u64) << 32;
(m | d) as i64
}
// Can remove once https://github.com/apache/arrow-rs/pull/2031 is released
/// Creates an IntervalMonthDayNano given its constituent components
///
/// Source: https://github.com/apache/arrow-rs/blob/e59b023480437f67e84ba2f827b58f78fd44c3a1/integration-testing/src/lib.rs#L340
/// ((nanoseconds as i128) & 0xFFFFFFFFFFFFFFFF) << 64
/// | ((days as i128) & 0xFFFFFFFF) << 32
/// | ((months as i128) & 0xFFFFFFFF);
fn create_month_day_nano(months: i32, days: i32, nanos: i64) -> i128 {
let m = months as u128 & u32::MAX as u128;
let d = (days as u128 & u32::MAX as u128) << 32;
let n = (nanos as u128) << 64;
(m | d | n) as i128
}
}