Skip to main content

reifydb_routine/function/meta/
type.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::{constraint::bytes::MaxBytes, container::utf8::Utf8Container, r#type::Type as ValueType};
6
7use crate::function::{Function, FunctionCapability, FunctionContext, FunctionInfo, error::FunctionError};
8
9pub struct Type {
10	info: FunctionInfo,
11}
12
13impl Default for Type {
14	fn default() -> Self {
15		Self::new()
16	}
17}
18
19impl Type {
20	pub fn new() -> Self {
21		Self {
22			info: FunctionInfo::new("meta::type"),
23		}
24	}
25}
26
27impl Function for Type {
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: &[ValueType]) -> ValueType {
37		ValueType::Utf8
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() != 1 {
46			return Err(FunctionError::ArityMismatch {
47				function: ctx.fragment.clone(),
48				expected: 1,
49				actual: args.len(),
50			});
51		}
52
53		let column = &args[0];
54		let col_type = column.data().get_type();
55		let type_name = col_type.to_string();
56		let row_count = column.data().len();
57
58		let result_data: Vec<String> = vec![type_name; row_count];
59
60		let final_data = ColumnData::Utf8 {
61			container: Utf8Container::new(result_data),
62			max_bytes: MaxBytes::MAX,
63		};
64
65		Ok(Columns::new(vec![Column::new(ctx.fragment.clone(), final_data)]))
66	}
67}