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::{ColumnWithName, buffer::ColumnBuffer, columns::Columns};
5use reifydb_type::value::{constraint::bytes::MaxBytes, container::utf8::Utf8Container, r#type::Type as ValueType};
6
7use crate::routine::{Function, FunctionKind, Routine, RoutineInfo, context::FunctionContext, error::RoutineError};
8
9pub struct Type {
10	info: RoutineInfo,
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: RoutineInfo::new("meta::type"),
23		}
24	}
25}
26
27impl<'a> Routine<FunctionContext<'a>> for Type {
28	fn info(&self) -> &RoutineInfo {
29		&self.info
30	}
31
32	fn return_type(&self, _input_types: &[ValueType]) -> ValueType {
33		ValueType::Utf8
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 col_type = column.get_type();
51		let type_name = col_type.to_string();
52		let row_count = column.len();
53
54		let result_data: Vec<String> = vec![type_name; row_count];
55
56		let final_data = ColumnBuffer::Utf8 {
57			container: Utf8Container::new(result_data),
58			max_bytes: MaxBytes::MAX,
59		};
60
61		Ok(Columns::new(vec![ColumnWithName::new(ctx.fragment.clone(), final_data)]))
62	}
63}
64
65impl Function for Type {
66	fn kinds(&self) -> &[FunctionKind] {
67		&[FunctionKind::Scalar]
68	}
69}