Skip to main content

reifydb_function/blob/
b64url.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::value::column::data::ColumnData;
5use reifydb_type::{
6	fragment::Fragment,
7	value::{blob::Blob, r#type::Type},
8};
9
10use crate::{ScalarFunction, ScalarFunctionContext, error::ScalarFunctionError, propagate_options};
11
12pub struct BlobB64url;
13
14impl BlobB64url {
15	pub fn new() -> Self {
16		Self
17	}
18}
19
20impl ScalarFunction for BlobB64url {
21	fn scalar(&self, ctx: ScalarFunctionContext) -> crate::error::ScalarFunctionResult<ColumnData> {
22		if let Some(result) = propagate_options(self, &ctx) {
23			return result;
24		}
25
26		let columns = ctx.columns;
27		let row_count = ctx.row_count;
28
29		// Validate exactly 1 argument
30		if columns.len() != 1 {
31			return Err(ScalarFunctionError::ArityMismatch {
32				function: ctx.fragment.clone(),
33				expected: 1,
34				actual: columns.len(),
35			});
36		}
37
38		let column = columns.get(0).unwrap();
39
40		match &column.data() {
41			ColumnData::Utf8 {
42				container,
43				..
44			} => {
45				let mut result_data = Vec::with_capacity(container.data().len());
46				let mut result_bitvec = Vec::with_capacity(row_count);
47
48				for i in 0..row_count {
49					if container.is_defined(i) {
50						let b64url_str = &container[i];
51						let blob = Blob::from_b64url(Fragment::internal(b64url_str))?;
52						result_data.push(blob);
53						result_bitvec.push(true);
54					} else {
55						result_data.push(Blob::empty());
56						result_bitvec.push(false);
57					}
58				}
59
60				Ok(ColumnData::blob_with_bitvec(result_data, result_bitvec))
61			}
62			other => Err(ScalarFunctionError::InvalidArgumentType {
63				function: ctx.fragment.clone(),
64				argument_index: 0,
65				expected: vec![Type::Utf8],
66				actual: other.get_type(),
67			}),
68		}
69	}
70
71	fn return_type(&self, _input_types: &[Type]) -> Type {
72		Type::Blob
73	}
74}