Skip to main content

reifydb_function/clock/
now.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 Now;
10
11impl Now {
12	pub fn new() -> Self {
13		Self {}
14	}
15}
16
17impl ScalarFunction for Now {
18	fn scalar(&self, ctx: ScalarFunctionContext) -> crate::ScalarFunctionResult<ColumnData> {
19		if let Some(result) = propagate_options(self, &ctx) {
20			return result;
21		}
22
23		let row_count = ctx.row_count;
24
25		if ctx.columns.len() != 0 {
26			return Err(ScalarFunctionError::ArityMismatch {
27				function: ctx.fragment.clone(),
28				expected: 0,
29				actual: ctx.columns.len(),
30			});
31		}
32
33		let millis = ctx.clock.now_millis() as i64;
34		let data = vec![millis; row_count];
35		let bitvec = vec![true; row_count];
36
37		Ok(ColumnData::int8_with_bitvec(data, bitvec))
38	}
39
40	fn return_type(&self, _input_types: &[Type]) -> Type {
41		Type::Int8
42	}
43}