mz_expr/scalar/func/impls/
byte.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use mz_ore::cast::CastFrom;
11use mz_repr::strconv;
12
13use crate::EvalError;
14
15sqlfunc!(
16    #[sqlname = "bytea_to_text"]
17    #[preserves_uniqueness = true]
18    #[inverse = to_unary!(super::CastStringToBytes)]
19    fn cast_bytes_to_string(a: &'a [u8]) -> String {
20        let mut buf = String::new();
21        strconv::format_bytes(&mut buf, a);
22        buf
23    }
24);
25
26sqlfunc!(
27    #[sqlname = "crc32_bytes"]
28    fn crc32_bytes<'a>(a: &'a [u8]) -> u32 {
29        crc32fast::hash(a)
30    }
31);
32
33sqlfunc!(
34    #[sqlname = "crc32_string"]
35    fn crc32_string<'a>(a: &'a str) -> u32 {
36        crc32_bytes(a.as_bytes())
37    }
38);
39
40sqlfunc!(
41    #[sqlname = "kafka_murmur2_bytes"]
42    fn kafka_murmur2_bytes<'a>(a: &'a [u8]) -> i32 {
43        i32::from_ne_bytes((murmur2::murmur2(a, murmur2::KAFKA_SEED) & 0x7fffffff).to_ne_bytes())
44    }
45);
46
47sqlfunc!(
48    #[sqlname = "kafka_murmur2_string"]
49    fn kafka_murmur2_string<'a>(a: &'a str) -> i32 {
50        kafka_murmur2_bytes(a.as_bytes())
51    }
52);
53
54sqlfunc!(
55    #[sqlname = "seahash_bytes"]
56    fn seahash_bytes<'a>(a: &'a [u8]) -> u64 {
57        seahash::hash(a)
58    }
59);
60
61sqlfunc!(
62    #[sqlname = "seahash_string"]
63    fn seahash_string<'a>(a: &'a str) -> u64 {
64        seahash_bytes(a.as_bytes())
65    }
66);
67
68sqlfunc!(
69    #[sqlname = "bit_count"]
70    fn bit_count_bytes<'a>(a: &'a [u8]) -> Result<i64, EvalError> {
71        let count: u64 = a.iter().map(|b| u64::cast_from(b.count_ones())).sum();
72        i64::try_from(count).or_else(|_| Err(EvalError::Int64OutOfRange(count.to_string().into())))
73    }
74);
75
76sqlfunc!(
77    #[sqlname = "bit_length"]
78    fn bit_length_bytes<'a>(a: &'a [u8]) -> Result<i32, EvalError> {
79        let val = a.len() * 8;
80        i32::try_from(val).or_else(|_| Err(EvalError::Int32OutOfRange(val.to_string().into())))
81    }
82);
83
84sqlfunc!(
85    #[sqlname = "octet_length"]
86    fn byte_length_bytes<'a>(a: &'a [u8]) -> Result<i32, EvalError> {
87        let val = a.len();
88        i32::try_from(val).or_else(|_| Err(EvalError::Int32OutOfRange(val.to_string().into())))
89    }
90);