reifydb_routine/function/is/
type.rs1use reifydb_core::value::column::{ColumnWithName, buffer::ColumnBuffer, columns::Columns};
5use reifydb_type::value::{Value, r#type::Type};
6
7use crate::routine::{Function, FunctionKind, Routine, RoutineInfo, context::FunctionContext, error::RoutineError};
8
9pub struct IsType {
10 info: RoutineInfo,
11}
12
13impl Default for IsType {
14 fn default() -> Self {
15 Self::new()
16 }
17}
18
19impl IsType {
20 pub fn new() -> Self {
21 Self {
22 info: RoutineInfo::new("is::type"),
23 }
24 }
25}
26
27impl<'a> Routine<FunctionContext<'a>> for IsType {
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() != 2 {
42 return Err(RoutineError::FunctionArityMismatch {
43 function: ctx.fragment.clone(),
44 expected: 2,
45 actual: args.len(),
46 });
47 }
48
49 let value_column = &args[0];
50 let type_column = &args[1];
51 let row_count = value_column.len();
52
53 let target_type = match type_column.get_value(0) {
57 Value::Any(boxed) => match boxed.as_ref() {
58 Value::Type(t) => t.clone(),
59 _ => {
60 return Err(RoutineError::FunctionInvalidArgumentType {
61 function: ctx.fragment.clone(),
62 argument_index: 1,
63 expected: vec![Type::Any],
64 actual: boxed.get_type(),
65 });
66 }
67 },
68 Value::None {
69 ..
70 } => Type::Option(Box::new(Type::Any)),
71 other => {
72 return Err(RoutineError::FunctionInvalidArgumentType {
73 function: ctx.fragment.clone(),
74 argument_index: 1,
75 expected: vec![Type::Any],
76 actual: other.get_type(),
77 });
78 }
79 };
80
81 let data: Vec<bool> = (0..row_count)
83 .map(|i| {
84 let vtype = value_column.get_value(i).get_type();
85 if target_type == Type::Option(Box::new(Type::Any)) {
86 vtype.is_option()
87 } else {
88 !vtype.is_option() && vtype.inner_type() == target_type.inner_type()
89 }
90 })
91 .collect();
92
93 Ok(Columns::new(vec![ColumnWithName::new(ctx.fragment.clone(), ColumnBuffer::bool(data))]))
94 }
95}
96
97impl Function for IsType {
98 fn kinds(&self) -> &[FunctionKind] {
99 &[FunctionKind::Scalar]
100 }
101}