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