reifydb_routine/function/identity/
id.rs1use 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 Id {
10 info: FunctionInfo,
11}
12
13impl Default for Id {
14 fn default() -> Self {
15 Self::new()
16 }
17}
18
19impl Id {
20 pub fn new() -> Self {
21 Self {
22 info: FunctionInfo::new("identity::id"),
23 }
24 }
25}
26
27impl Function for Id {
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::IdentityId
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 identity = ctx.identity;
50 let row_count = ctx.row_count.max(1);
51 if identity.is_anonymous() {
52 return Ok(Columns::new(vec![Column::new(
53 ctx.fragment.clone(),
54 ColumnData::none_typed(Type::IdentityId, row_count),
55 )]));
56 }
57
58 Ok(Columns::new(vec![Column::new(
59 ctx.fragment.clone(),
60 ColumnData::identity_id(vec![identity; row_count]),
61 )]))
62 }
63}