|
16 | 16 | // under the License. |
17 | 17 |
|
18 | 18 | use crate::utils::array_with_timezone; |
19 | | -use arrow::array::ArrayRef; |
| 19 | +use arrow::array::{new_null_array, ArrayRef}; |
20 | 20 | use arrow::compute::cast; |
21 | 21 | use arrow::datatypes::{DataType, Schema, TimeUnit::Microsecond}; |
22 | 22 | use arrow::record_batch::RecordBatch; |
@@ -142,6 +142,18 @@ impl PhysicalExpr for TimestampTruncExpr { |
142 | 142 | let scalar = ScalarValue::try_from_array(&relabel(result)?, 0)?; |
143 | 143 | Ok(ColumnarValue::Scalar(scalar)) |
144 | 144 | } |
| 145 | + (ColumnarValue::Scalar(ts_scalar), ColumnarValue::Array(formats)) => { |
| 146 | + let ts = ts_scalar.to_array_of_size(formats.len())?; |
| 147 | + let result = timestamp_trunc_array_fmt_dyn(&resolve_tz(ts)?, &formats, wrap)?; |
| 148 | + Ok(ColumnarValue::Array(relabel(result)?)) |
| 149 | + } |
| 150 | + // A NULL format gives NULL, as in Spark. |
| 151 | + (ColumnarValue::Array(ts), ColumnarValue::Scalar(Utf8(None))) => { |
| 152 | + Ok(ColumnarValue::Array(new_null_array(&output_type, ts.len()))) |
| 153 | + } |
| 154 | + (ColumnarValue::Scalar(_), ColumnarValue::Scalar(Utf8(None))) => { |
| 155 | + Ok(ColumnarValue::Scalar(ScalarValue::try_from(&output_type)?)) |
| 156 | + } |
145 | 157 | _ => Err(DataFusionError::Execution( |
146 | 158 | "Invalid input to function TimestampTrunc. \ |
147 | 159 | Expected (Timestamp, Utf8)" |
@@ -184,7 +196,9 @@ fn output_type(input: &DataType) -> DataType { |
184 | 196 | #[cfg(test)] |
185 | 197 | mod tests { |
186 | 198 | use super::*; |
187 | | - use arrow::array::{Array, AsArray, DictionaryArray, Int32Array, TimestampMicrosecondArray}; |
| 199 | + use arrow::array::{ |
| 200 | + Array, AsArray, DictionaryArray, Int32Array, StringArray, TimestampMicrosecondArray, |
| 201 | + }; |
188 | 202 | use arrow::datatypes::{Field, Int32Type, TimestampMicrosecondType}; |
189 | 203 | use datafusion::physical_expr::expressions::{Column, Literal}; |
190 | 204 |
|
@@ -241,4 +255,68 @@ mod tests { |
241 | 255 | HOUR_IN_KOLKATA |
242 | 256 | ); |
243 | 257 | } |
| 258 | + |
| 259 | + /// Evaluates `date_trunc` in America/Los_Angeles with the given timestamp and format inputs. |
| 260 | + fn trunc_in_los_angeles( |
| 261 | + timestamp: Arc<dyn PhysicalExpr>, |
| 262 | + format: Arc<dyn PhysicalExpr>, |
| 263 | + batch: &RecordBatch, |
| 264 | + ) -> ColumnarValue { |
| 265 | + TimestampTruncExpr::new(timestamp, format, "America/Los_Angeles".to_string(), false) |
| 266 | + .evaluate(batch) |
| 267 | + .unwrap() |
| 268 | + } |
| 269 | + |
| 270 | + #[test] |
| 271 | + fn literal_timestamp_with_a_format_column() { |
| 272 | + // 2024-11-03 01:30 PDT, inside the overlap. DAY keeps the input's offset, YEAR does not |
| 273 | + // depend on it, and a NULL format gives NULL. |
| 274 | + let micros = 1_730_622_600_000_000; |
| 275 | + let formats = StringArray::from(vec![Some("DAY"), Some("YEAR"), None]); |
| 276 | + let schema = Schema::new(vec![Field::new("fmt", DataType::Utf8, true)]); |
| 277 | + let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(formats)]).unwrap(); |
| 278 | + let timestamp = Literal::new(ScalarValue::TimestampMicrosecond( |
| 279 | + Some(micros), |
| 280 | + Some("UTC".into()), |
| 281 | + )); |
| 282 | + let ColumnarValue::Array(result) = |
| 283 | + trunc_in_los_angeles(Arc::new(timestamp), Arc::new(Column::new("fmt", 0)), &batch) |
| 284 | + else { |
| 285 | + panic!("expected an array"); |
| 286 | + }; |
| 287 | + assert_eq!(result.data_type(), &utc_timestamp()); |
| 288 | + let result = result.as_primitive::<TimestampMicrosecondType>(); |
| 289 | + // 2024-11-03 00:00 PDT and 2024-01-01 00:00 PST. |
| 290 | + assert_eq!(result.value(0), 1_730_617_200_000_000); |
| 291 | + assert_eq!(result.value(1), 1_704_096_000_000_000); |
| 292 | + assert!(result.is_null(2)); |
| 293 | + } |
| 294 | + |
| 295 | + #[test] |
| 296 | + fn null_literal_format_gives_null() { |
| 297 | + let input = TimestampMicrosecondArray::from(vec![Some(MICROS), None]).with_timezone("UTC"); |
| 298 | + let schema = Schema::new(vec![Field::new("ts", utc_timestamp(), true)]); |
| 299 | + let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(input)]).unwrap(); |
| 300 | + let null_format = || Arc::new(Literal::new(Utf8(None))); |
| 301 | + let ColumnarValue::Array(result) = |
| 302 | + trunc_in_los_angeles(Arc::new(Column::new("ts", 0)), null_format(), &batch) |
| 303 | + else { |
| 304 | + panic!("expected an array"); |
| 305 | + }; |
| 306 | + assert_eq!(result.data_type(), &utc_timestamp()); |
| 307 | + assert_eq!(result.null_count(), 2); |
| 308 | + |
| 309 | + let timestamp = Arc::new(Literal::new(ScalarValue::TimestampMicrosecond( |
| 310 | + Some(MICROS), |
| 311 | + Some("UTC".into()), |
| 312 | + ))); |
| 313 | + let ColumnarValue::Scalar(result) = trunc_in_los_angeles(timestamp, null_format(), &batch) |
| 314 | + else { |
| 315 | + panic!("expected a scalar"); |
| 316 | + }; |
| 317 | + assert_eq!( |
| 318 | + result, |
| 319 | + ScalarValue::TimestampMicrosecond(None, Some("UTC".into())) |
| 320 | + ); |
| 321 | + } |
244 | 322 | } |
0 commit comments