reifydb_routine/function/is/
type.rs1use reifydb_core::value::column::{Column, columns::Columns, data::ColumnData};
5use reifydb_type::value::{Value, r#type::Type};
6
7use crate::function::{Function, FunctionCapability, FunctionContext, FunctionInfo, error::FunctionError};
8
9pub struct IsType {
10 info: FunctionInfo,
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: FunctionInfo::new("is::type"),
23 }
24 }
25}
26
27impl Function for IsType {
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 propagates_options(&self) -> bool {
41 false
42 }
43
44 fn execute(&self, ctx: &FunctionContext, args: &Columns) -> Result<Columns, FunctionError> {
45 if args.len() != 2 {
46 return Err(FunctionError::ArityMismatch {
47 function: ctx.fragment.clone(),
48 expected: 2,
49 actual: args.len(),
50 });
51 }
52
53 let value_column = &args[0];
54 let type_column = &args[1];
55 let row_count = value_column.data().len();
56
57 let target_type = match type_column.data().get_value(0) {
61 Value::Any(boxed) => match boxed.as_ref() {
62 Value::Type(t) => t.clone(),
63 _ => {
64 return Err(FunctionError::InvalidArgumentType {
65 function: ctx.fragment.clone(),
66 argument_index: 1,
67 expected: vec![Type::Any],
68 actual: boxed.get_type(),
69 });
70 }
71 },
72 Value::None {
73 ..
74 } => Type::Option(Box::new(Type::Any)),
75 other => {
76 return Err(FunctionError::InvalidArgumentType {
77 function: ctx.fragment.clone(),
78 argument_index: 1,
79 expected: vec![Type::Any],
80 actual: other.get_type(),
81 });
82 }
83 };
84
85 let data: Vec<bool> = (0..row_count)
87 .map(|i| {
88 let vtype = value_column.data().get_value(i).get_type();
89 if target_type == Type::Option(Box::new(Type::Any)) {
90 vtype.is_option()
91 } else {
92 !vtype.is_option() && vtype.inner_type() == target_type.inner_type()
93 }
94 })
95 .collect();
96
97 Ok(Columns::new(vec![Column::new(ctx.fragment.clone(), ColumnData::bool(data))]))
98 }
99}