reifydb_routine/function/is/
some.rs1use reifydb_core::value::column::{ColumnWithName, buffer::ColumnBuffer, columns::Columns};
5use reifydb_type::value::r#type::Type;
6
7use crate::routine::{Function, FunctionKind, Routine, RoutineInfo, context::FunctionContext, error::RoutineError};
8
9pub struct IsSome {
10 info: RoutineInfo,
11}
12
13impl Default for IsSome {
14 fn default() -> Self {
15 Self::new()
16 }
17}
18
19impl IsSome {
20 pub fn new() -> Self {
21 Self {
22 info: RoutineInfo::new("is::some"),
23 }
24 }
25}
26
27impl<'a> Routine<FunctionContext<'a>> for IsSome {
28 fn info(&self) -> &RoutineInfo {
29 &self.info
30 }
31
32 fn return_type(&self, _input_types: &[Type]) -> Type {
33 Type::Boolean
34 }
35
36 fn propagates_options(&self) -> bool {
37 false
38 }
39
40 fn execute(&self, ctx: &mut FunctionContext<'a>, args: &Columns) -> Result<Columns, RoutineError> {
41 if args.len() != 1 {
42 return Err(RoutineError::FunctionArityMismatch {
43 function: ctx.fragment.clone(),
44 expected: 1,
45 actual: args.len(),
46 });
47 }
48
49 let column = &args[0];
50 let row_count = column.len();
51 let data: Vec<bool> = (0..row_count).map(|i| column.is_defined(i)).collect();
52
53 Ok(Columns::new(vec![ColumnWithName::new(ctx.fragment.clone(), ColumnBuffer::bool(data))]))
54 }
55}
56
57impl Function for IsSome {
58 fn kinds(&self) -> &[FunctionKind] {
59 &[FunctionKind::Scalar]
60 }
61}