Skip to content

Commit

Permalink
Merge pull request #12 from yanhuangdata/feature/conv-error
Browse files Browse the repository at this point in the history
Enhance error handling for `conv` function
  • Loading branch information
ZMZ91 authored Mar 18, 2024
2 parents fd32b57 + c8714a4 commit 2e83522
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 52 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# 0.3.1 (2024-03-17)
* Enhance `conv` function to handle more errors
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gandiva_rust_udf"
version = "0.2.0"
version = "0.3.1"
edition = "2021"
authors = ["yanhuangdata"]
description = "A library for gandiva rust udfs"
Expand All @@ -26,3 +26,8 @@ members = [
"bar_func",
]
resolver = "2"

[workspace.dependencies]
libc = "0.2.152"
gandiva_rust_udf_macro = { version = "0.1.4" }
gandiva_rust_udf_shared = { version = "0.1.4" }
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,31 @@ crate-type = ["cdylib"]
members = [
"my_func",
]

[dependencies]
libc = "0.2.152"
gandiva_rust_udf_macro = { version = "0.1.3" }
gandiva_rust_udf_shared = { version = "0.1.2" }
```
- go to gandiva_rust_udf/my_func/Cargo.toml and add gandiva_rust_udf_macro and gandiva_rust_udf_shared in dependencies
```toml
# in gandiva_rust_udf/my_func/Cargo.toml
...
[package]
name = "my_func"
version = "0.0.1"
edition = "2021"

[lib]
name = "my_func"
path = "src/lib.rs"

[dependencies]
gandiva_rust_udf_macro = { version = "0.1.3" }
gandiva_rust_udf_shared = { version = "0.1.2" }
...
# if your function requires string as parameters or return value, you should add libc as a dependency
libc = { workspace = true }
gandiva_rust_udf_macro = { workspace = true }
gandiva_rust_udf_shared = { workspace = true }
```

- code the function in gandiva_rust_udf/my_func/src/lib.rs for example
```rust
use gandiva_rust_udf_macro::udf;
Expand Down
6 changes: 3 additions & 3 deletions bar_func/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ name = "bar_func"
path = "src/lib.rs"

[dependencies]
libc = "0.2.152"
gandiva_rust_udf_macro = { version = "0.1.1" }
gandiva_rust_udf_shared = { version = "0.1.1" }
libc = { workspace = true }
gandiva_rust_udf_macro = { workspace = true }
gandiva_rust_udf_shared = { workspace = true }
6 changes: 3 additions & 3 deletions conv_func/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
libc = "0.2.152"
gandiva_rust_udf_macro = { version = "0.1.3" }
gandiva_rust_udf_shared = { version = "0.1.2" }
libc = { workspace = true }
gandiva_rust_udf_macro = { workspace = true }
gandiva_rust_udf_shared = { workspace = true }
radix_fmt = "1.0.0"
65 changes: 47 additions & 18 deletions conv_func/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,96 @@ use gandiva_rust_udf_macro::udf;
use radix_fmt::radix;

#[udf]
pub fn conv(num: &str, from_radix: i64, to_radix: i64) -> String {
pub fn conv(num: &str, from_radix: i64, to_radix: i64) -> Result<String, String> {
if (from_radix < 2 || from_radix > 36) || (to_radix < 2 || to_radix > 36) {
// return an error with the actual radix given as part of the error message
return Err(format!(
"Radix must be between 2 and 36, got from_radix: {}, to_radix: {}",
from_radix, to_radix
));
}
let value = i64::from_str_radix(num, from_radix as u32);
match value {
Ok(v) => radix(v, to_radix as u8).to_string(),
Err(_) => String::from(""),
Ok(v) => Ok(radix(v, to_radix as u8).to_string()),
Err(e) => Err(e.to_string()),
}
}

#[cfg(test)]
mod tests {
mod conv_tests {
use super::*;

fn assert_conv(num: &str, from_radix: i64, to_radix: i64, expected: &str) {
let result = conv(num, from_radix, to_radix);
assert!(result.is_ok());
let value = result.unwrap();
assert_eq!(value, expected);
}

fn assert_conv_err(num: &str, from_radix: i64, to_radix: i64, expected: &str) {
let result = conv(num, from_radix, to_radix);
assert!(result.is_err());
let value = result.err().unwrap();
assert_eq!(value, expected);
}

#[test]
fn test_conv_binary_to_decimal() {
let result = conv("100", 2, 10);
assert_eq!(result, "4");
assert_conv("100", 2, 10, "4");
}

#[test]
fn test_conv_decimal_to_binary() {
assert_eq!(conv("5", 10, 2), "101");
assert_conv("5", 10, 2, "101");
}

#[test]
fn test_conv_binary_to_hex() {
let result = conv("1110", 2, 16);
assert_eq!(result, "e");
assert_conv("1110", 2, 16, "e");
}

#[test]
fn test_conv_hex_to_binary() {
let result = conv("e", 16, 2);
assert_eq!(result, "1110");
assert_conv("e", 16, 2, "1110");
}

#[test]
fn test_conv_decimal_to_hex() {
assert_eq!(conv("255", 10, 16), "ff");
assert_conv("255", 10, 16, "ff");
}

#[test]
fn test_conv_hex_to_decimal() {
assert_eq!(conv("A", 16, 10), "10");
assert_conv("A", 16, 10, "10");
}

#[test]
fn test_conv_hexatridecimal_to_decimal() {
assert_eq!(conv("21", 36, 10), "73");
assert_conv("21", 36, 10, "73");
}

#[test]
fn test_conv_hexatridecimal_without_num_to_decima() {
assert_eq!(conv("HELLO", 36, 10), "29234652");
assert_conv("HELLO", 36, 10, "29234652");
}

#[test]
fn test_empty_str_conv() {
assert_eq!(conv("", 2, 16), "");
assert_conv_err("", 2, 16, "cannot parse integer from empty string");
}

#[test]
fn test_str_invalid_digit() {
assert_conv_err("2", 2, 10, "invalid digit found in string");
}

#[test]
fn test_str_without_num_conv() {
assert_eq!(conv("HELLO", 2, 10), "");
fn test_from_radix_too_large() {
assert_conv_err(
"2",
37,
10,
"Radix must be between 2 and 36, got from_radix: 37, to_radix: 10",
);
}
}
6 changes: 3 additions & 3 deletions format_func/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ name = "format_func"
path = "src/lib.rs"

[dependencies]
libc = "0.2.152"
gandiva_rust_udf_macro = { version = "0.1.3" }
gandiva_rust_udf_shared = { version = "0.1.2" }
libc = { workspace = true }
gandiva_rust_udf_macro = { workspace = true }
gandiva_rust_udf_shared = { workspace = true }
strfmt = "0.2.4"
6 changes: 3 additions & 3 deletions ip_func/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
libc = "0.2.152"
gandiva_rust_udf_macro = { version = "0.1.3" }
gandiva_rust_udf_shared = { version = "0.1.2" }
libc = { workspace = true }
gandiva_rust_udf_macro = { workspace = true }
gandiva_rust_udf_shared = { workspace = true }
6 changes: 3 additions & 3 deletions is_ascii_func/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
libc = "0.2.152"
gandiva_rust_udf_macro = { version = "0.1.3" }
gandiva_rust_udf_shared = { version = "0.1.2" }
libc = { workspace = true }
gandiva_rust_udf_macro = { workspace = true }
gandiva_rust_udf_shared = { workspace = true }
5 changes: 3 additions & 2 deletions num_func/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
num-integer = "0.1.45"
gandiva_rust_udf_macro = { version = "0.1.3" }
gandiva_rust_udf_shared = { version = "0.1.2" }
libc = { workspace = true }
gandiva_rust_udf_macro = { workspace = true }
gandiva_rust_udf_shared = { workspace = true }
6 changes: 3 additions & 3 deletions strsim_func/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
strsim = "0.11.0"
libc = "0.2.152"
gandiva_rust_udf_macro = { version = "0.1.3" }
gandiva_rust_udf_shared = { version = "0.1.2" }
libc = { workspace = true }
gandiva_rust_udf_macro = { workspace = true }
gandiva_rust_udf_shared = { workspace = true }
6 changes: 3 additions & 3 deletions url_func/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
libc = "0.2.152"
gandiva_rust_udf_macro = { version = "0.1.3" }
gandiva_rust_udf_shared = { version = "0.1.2" }
libc = { workspace = true }
gandiva_rust_udf_macro = { workspace = true }
gandiva_rust_udf_shared = { workspace = true }
url = "2.4.0"
6 changes: 3 additions & 3 deletions uuid_func/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
libc = "0.2.152"
gandiva_rust_udf_macro = { version = "0.1.3" }
gandiva_rust_udf_shared = { version = "0.1.2" }
libc = { workspace = true }
gandiva_rust_udf_macro = { workspace = true }
gandiva_rust_udf_shared = { workspace = true }

[dependencies.uuid]
version = "1.3.3"
Expand Down
6 changes: 3 additions & 3 deletions valid_json_func/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde_json = "1.0.95"
libc = "0.2.152"
gandiva_rust_udf_macro = { version = "0.1.3" }
gandiva_rust_udf_shared = { version = "0.1.2" }
libc = { workspace = true }
gandiva_rust_udf_macro = { workspace = true }
gandiva_rust_udf_shared = { workspace = true }

0 comments on commit 2e83522

Please sign in to comment.