Skip to main content

reifydb_routine/function/blob/
b64url.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::{
6	fragment::Fragment,
7	value::{blob::Blob, r#type::Type},
8};
9
10use crate::function::{Function, FunctionCapability, FunctionContext, FunctionInfo, error::FunctionError};
11
12pub struct BlobB64url {
13	info: FunctionInfo,
14}
15
16impl Default for BlobB64url {
17	fn default() -> Self {
18		Self::new()
19	}
20}
21
22impl BlobB64url {
23	pub fn new() -> Self {
24		Self {
25			info: FunctionInfo::new("blob::b64url"),
26		}
27	}
28}
29
30impl Function for BlobB64url {
31	fn info(&self) -> &FunctionInfo {
32		&self.info
33	}
34
35	fn capabilities(&self) -> &[FunctionCapability] {
36		&[FunctionCapability::Scalar]
37	}
38
39	fn return_type(&self, _input_types: &[Type]) -> Type {
40		Type::Blob
41	}
42
43	fn execute(&self, ctx: &FunctionContext, args: &Columns) -> Result<Columns, FunctionError> {
44		if args.len() != 1 {
45			return Err(FunctionError::ArityMismatch {
46				function: ctx.fragment.clone(),
47				expected: 1,
48				actual: args.len(),
49			});
50		}
51
52		let column = &args[0];
53		let (data, bitvec) = column.data().unwrap_option();
54		let row_count = data.len();
55
56		match data {
57			ColumnData::Utf8 {
58				container,
59				..
60			} => {
61				let mut result_data = Vec::with_capacity(container.data().len());
62				let mut result_bitvec = Vec::with_capacity(row_count);
63
64				for i in 0..row_count {
65					if container.is_defined(i) {
66						let b64url_str = &container[i];
67						let blob = Blob::from_b64url(Fragment::internal(b64url_str))?;
68						result_data.push(blob);
69						result_bitvec.push(true);
70					} else {
71						result_data.push(Blob::empty());
72						result_bitvec.push(false);
73					}
74				}
75
76				let result_col_data = ColumnData::blob_with_bitvec(result_data, result_bitvec);
77				let final_data = match bitvec {
78					Some(bv) => ColumnData::Option {
79						inner: Box::new(result_col_data),
80						bitvec: bv.clone(),
81					},
82					None => result_col_data,
83				};
84				Ok(Columns::new(vec![Column::new(ctx.fragment.clone(), final_data)]))
85			}
86			other => Err(FunctionError::InvalidArgumentType {
87				function: ctx.fragment.clone(),
88				argument_index: 0,
89				expected: vec![Type::Utf8],
90				actual: other.get_type(),
91			}),
92		}
93	}
94}