datafusion_functions/string/
uuid.rs1use std::any::Any;
19use std::sync::Arc;
20
21use arrow::array::GenericStringBuilder;
22use arrow::datatypes::DataType;
23use arrow::datatypes::DataType::Utf8;
24use rand::Rng;
25use uuid::Uuid;
26
27use datafusion_common::{internal_err, Result};
28use datafusion_expr::{ColumnarValue, Documentation, Volatility};
29use datafusion_expr::{ScalarFunctionArgs, ScalarUDFImpl, Signature};
30use datafusion_macros::user_doc;
31
32#[user_doc(
33 doc_section(label = "String Functions"),
34 description = "Returns [`UUID v4`](https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)) string value which is unique per row.",
35 syntax_example = "uuid()",
36 sql_example = r#"```sql
37> select uuid();
38+--------------------------------------+
39| uuid() |
40+--------------------------------------+
41| 6ec17ef8-1934-41cc-8d59-d0c8f9eea1f0 |
42+--------------------------------------+
43```"#
44)]
45#[derive(Debug)]
46pub struct UuidFunc {
47 signature: Signature,
48}
49
50impl Default for UuidFunc {
51 fn default() -> Self {
52 Self::new()
53 }
54}
55
56impl UuidFunc {
57 pub fn new() -> Self {
58 Self {
59 signature: Signature::exact(vec![], Volatility::Volatile),
60 }
61 }
62}
63
64impl ScalarUDFImpl for UuidFunc {
65 fn as_any(&self) -> &dyn Any {
66 self
67 }
68
69 fn name(&self) -> &str {
70 "uuid"
71 }
72
73 fn signature(&self) -> &Signature {
74 &self.signature
75 }
76
77 fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
78 Ok(Utf8)
79 }
80
81 fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
84 if !args.args.is_empty() {
85 return internal_err!("{} function does not accept arguments", self.name());
86 }
87
88 let mut rng = rand::thread_rng();
90 let mut randoms = vec![0u128; args.number_rows];
91 rng.fill(&mut randoms[..]);
92
93 let mut builder = GenericStringBuilder::<i32>::with_capacity(
94 args.number_rows,
95 args.number_rows * 36,
96 );
97
98 let mut buffer = [0u8; 36];
99 for x in &mut randoms {
100 *x = *x & 0xFFFFFFFFFFFF4FFFBFFFFFFFFFFFFFFF | 0x40008000000000000000;
102 let uuid = Uuid::from_u128(*x);
103 let fmt = uuid::fmt::Hyphenated::from_uuid(uuid);
104 builder.append_value(fmt.encode_lower(&mut buffer));
105 }
106
107 Ok(ColumnarValue::Array(Arc::new(builder.finish())))
108 }
109
110 fn documentation(&self) -> Option<&Documentation> {
111 self.doc()
112 }
113}