risingwave_expr_impl/scalar/
md5.rs1use std::fmt::Write;
16
17use risingwave_expr::function;
18
19#[function("md5(varchar) -> varchar")]
20pub fn md5(s: &str, writer: &mut impl Write) {
21 write!(writer, "{:x}", ::md5::compute(s)).unwrap();
22}
23
24#[function("md5(bytea) -> varchar")]
25pub fn md5_from_bytea(s: &[u8], writer: &mut impl Write) {
26 writer
27 .write_str(&::hex::encode(::md5::compute(s).0))
28 .unwrap();
29}
30
31#[cfg(test)]
32mod tests {
33 use super::*;
34
35 #[test]
36 fn test_md5() {
37 let cases = [
38 ("hello world", "5eb63bbbe01eeed093cb22bb8f5acdc3"),
39 ("hello RUST", "917b821a0a5f23ab0cfdb36056d2eb9d"),
40 (
41 "abcdefghijklmnopqrstuvwxyz",
42 "c3fcd3d76192e4007dfb496cca67e13b",
43 ),
44 ];
45
46 for (s, expected) in cases {
47 let mut writer = String::new();
48 md5(s, &mut writer);
49 assert_eq!(writer, expected);
50 }
51 }
52
53 #[test]
54 fn test_md5_bytea() {
55 let cases = [
56 ("hello world".as_bytes(), "5eb63bbbe01eeed093cb22bb8f5acdc3"),
57 ("hello RUST".as_bytes(), "917b821a0a5f23ab0cfdb36056d2eb9d"),
58 (
59 "abcdefghijklmnopqrstuvwxyz".as_bytes(),
60 "c3fcd3d76192e4007dfb496cca67e13b",
61 ),
62 ];
63
64 for (s, expected) in cases {
65 let mut writer = String::new();
66 md5_from_bytea(s, &mut writer);
67 assert_eq!(writer, expected);
68 }
69 }
70}