Skip to main content

reifydb_function/datetime/
epoch.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::value::column::data::ColumnData;
5use reifydb_type::value::r#type::Type;
6
7use crate::{ScalarFunction, ScalarFunctionContext, error::ScalarFunctionError, propagate_options};
8
9pub struct DateTimeEpoch;
10
11impl DateTimeEpoch {
12	pub fn new() -> Self {
13		Self
14	}
15}
16
17impl ScalarFunction for DateTimeEpoch {
18	fn scalar(&self, ctx: ScalarFunctionContext) -> crate::error::ScalarFunctionResult<ColumnData> {
19		if let Some(result) = propagate_options(self, &ctx) {
20			return result;
21		}
22		let columns = ctx.columns;
23		let row_count = ctx.row_count;
24
25		if columns.len() != 1 {
26			return Err(ScalarFunctionError::ArityMismatch {
27				function: ctx.fragment.clone(),
28				expected: 1,
29				actual: columns.len(),
30			});
31		}
32
33		let col = columns.get(0).unwrap();
34
35		match col.data() {
36			ColumnData::DateTime(container) => {
37				let mut data = Vec::with_capacity(row_count);
38				let mut bitvec = Vec::with_capacity(row_count);
39
40				for i in 0..row_count {
41					if let Some(dt) = container.get(i) {
42						data.push(dt.timestamp());
43						bitvec.push(true);
44					} else {
45						data.push(0);
46						bitvec.push(false);
47					}
48				}
49
50				Ok(ColumnData::int8_with_bitvec(data, bitvec))
51			}
52			other => Err(ScalarFunctionError::InvalidArgumentType {
53				function: ctx.fragment.clone(),
54				argument_index: 0,
55				expected: vec![Type::DateTime],
56				actual: other.get_type(),
57			}),
58		}
59	}
60
61	fn return_type(&self, _input_types: &[Type]) -> Type {
62		Type::Int8
63	}
64}