Skip to main content

reifydb_routine/function/is/
root.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::value::column::{Column, columns::Columns, data::ColumnData};
5use reifydb_type::value::r#type::Type;
6
7use crate::function::{Function, FunctionCapability, FunctionContext, FunctionInfo, error::FunctionError};
8
9pub struct IsRoot {
10	info: FunctionInfo,
11}
12
13impl Default for IsRoot {
14	fn default() -> Self {
15		Self::new()
16	}
17}
18
19impl IsRoot {
20	pub fn new() -> Self {
21		Self {
22			info: FunctionInfo::new("is::root"),
23		}
24	}
25}
26
27impl Function for IsRoot {
28	fn info(&self) -> &FunctionInfo {
29		&self.info
30	}
31
32	fn capabilities(&self) -> &[FunctionCapability] {
33		&[FunctionCapability::Scalar]
34	}
35
36	fn return_type(&self, _input_types: &[Type]) -> Type {
37		Type::Boolean
38	}
39
40	fn execute(&self, ctx: &FunctionContext, args: &Columns) -> Result<Columns, FunctionError> {
41		if !args.is_empty() {
42			return Err(FunctionError::ArityMismatch {
43				function: ctx.fragment.clone(),
44				expected: 0,
45				actual: args.len(),
46			});
47		}
48
49		let is_root = ctx.identity.is_root();
50		let row_count = ctx.row_count.max(1);
51		let data: Vec<bool> = vec![is_root; row_count];
52
53		Ok(Columns::new(vec![Column::new(ctx.fragment.clone(), ColumnData::bool(data))]))
54	}
55}