This is an automated email from the ASF dual-hosted git repository.
aitozi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/paimon-rust.git
The following commit(s) were added to refs/heads/main by this push:
new e86c828 refactor: Refactor error in DataType (#36)
e86c828 is described below
commit e86c828343f7da1492572bb3328d5780f1139add
Author: ForwardXu <[email protected]>
AuthorDate: Sun Aug 4 22:04:39 2024 +0800
refactor: Refactor error in DataType (#36)
---
crates/paimon/src/error.rs | 5 ++
crates/paimon/src/spec/types.rs | 127 ++++++++++++++++++++++++----------------
2 files changed, 82 insertions(+), 50 deletions(-)
diff --git a/crates/paimon/src/error.rs b/crates/paimon/src/error.rs
index 6323f3f..93404da 100644
--- a/crates/paimon/src/error.rs
+++ b/crates/paimon/src/error.rs
@@ -29,6 +29,11 @@ pub enum Error {
#[snafu(backtrace)]
source: snafu::Whatever,
},
+ #[snafu(
+ visibility(pub(crate)),
+ display("Paimon data type invalid for {}", message)
+ )]
+ DataTypeInvalid { message: String },
#[snafu(
visibility(pub(crate)),
display("Paimon hitting unexpected error {}: {:?}", message, source)
diff --git a/crates/paimon/src/spec/types.rs b/crates/paimon/src/spec/types.rs
index 63487ed..84d7115 100644
--- a/crates/paimon/src/spec/types.rs
+++ b/crates/paimon/src/spec/types.rs
@@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.
-use crate::error::Error;
+use crate::error::*;
use crate::spec::DataField;
use bitflags::bitflags;
use serde::{Deserialize, Serialize};
@@ -278,13 +278,16 @@ impl BinaryType {
pub const DEFAULT_LENGTH: usize = 1;
- pub fn new(length: usize) -> Result<Self, &'static str> {
+ pub fn new(length: usize) -> Result<Self, Error> {
Self::with_nullable(true, length)
}
- pub fn with_nullable(nullable: bool, length: usize) -> Result<Self,
&'static str> {
+ pub fn with_nullable(nullable: bool, length: usize) -> Result<Self, Error>
{
if length < Self::MIN_LENGTH {
- return Err("Binary string length must be at least 1.");
+ return DataTypeInvalidSnafu {
+ message: "Binary string length must be at least
1.".to_string(),
+ }
+ .fail();
}
Ok(Self { nullable, length })
}
@@ -373,13 +376,16 @@ impl CharType {
pub const MAX_LENGTH: usize = 255;
- pub fn new(length: usize) -> Result<Self, &'static str> {
+ pub fn new(length: usize) -> Result<Self, Error> {
Self::with_nullable(true, length)
}
- pub fn with_nullable(nullable: bool, length: usize) -> Result<Self,
&'static str> {
+ pub fn with_nullable(nullable: bool, length: usize) -> Result<Self, Error>
{
if !(Self::MIN_LENGTH..=Self::MAX_LENGTH).contains(&length) {
- return Err("Character string length must be between 1 and 255
(both inclusive).");
+ return DataTypeInvalidSnafu {
+ message: "Char string length must be between 1 and
255.".to_string(),
+ }
+ .fail();
}
Ok(CharType { nullable, length })
}
@@ -473,25 +479,31 @@ impl DecimalType {
pub const DEFAULT_SCALE: u32 = 0;
- pub fn new(precision: u32, scale: u32) -> Result<Self, String> {
+ pub fn new(precision: u32, scale: u32) -> Result<Self, Error> {
Self::with_nullable(true, precision, scale)
}
- pub fn with_nullable(nullable: bool, precision: u32, scale: u32) ->
Result<Self, String> {
+ pub fn with_nullable(nullable: bool, precision: u32, scale: u32) ->
Result<Self, Error> {
if !(Self::MIN_PRECISION..=Self::MAX_PRECISION).contains(&precision) {
- return Err(format!(
- "Decimal precision must be between {} and {} (both
inclusive).",
- Self::MIN_PRECISION,
- Self::MAX_PRECISION
- ));
+ return DataTypeInvalidSnafu {
+ message: format!(
+ "Decimal precision must be between {} and {} (both
inclusive).",
+ Self::MIN_PRECISION,
+ Self::MAX_PRECISION
+ ),
+ }
+ .fail();
}
if !(Self::MIN_SCALE..=precision).contains(&scale) {
- return Err(format!(
- "Decimal scale must be between {} and the precision {} (both
inclusive).",
- Self::MIN_SCALE,
- precision
- ));
+ return DataTypeInvalidSnafu {
+ message: format!(
+ "Decimal scale must be between {} and {} (both
inclusive).",
+ Self::MIN_SCALE,
+ precision
+ ),
+ }
+ .fail();
}
Ok(DecimalType {
@@ -669,17 +681,20 @@ impl LocalZonedTimestampType {
pub const DEFAULT_PRECISION: u32 = TimestampType::DEFAULT_PRECISION;
- pub fn new(precision: u32) -> Result<Self, String> {
+ pub fn new(precision: u32) -> Result<Self, Error> {
Self::with_nullable(true, precision)
}
- pub fn with_nullable(nullable: bool, precision: u32) -> Result<Self,
String> {
+ pub fn with_nullable(nullable: bool, precision: u32) -> Result<Self,
Error> {
if !(Self::MIN_PRECISION..=Self::MAX_PRECISION).contains(&precision) {
- return Err(format!(
- "Timestamp precision must be between {} and {} (both
inclusive).",
- Self::MIN_PRECISION,
- Self::MAX_PRECISION
- ));
+ return DataTypeInvalidSnafu {
+ message: format!(
+ "LocalZonedTimestamp precision must be between {} and {}
(both inclusive).",
+ Self::MIN_PRECISION,
+ Self::MAX_PRECISION
+ ),
+ }
+ .fail();
}
Ok(LocalZonedTimestampType {
@@ -778,17 +793,20 @@ impl TimeType {
pub const DEFAULT_PRECISION: u32 = 0;
- pub fn new(precision: u32) -> Result<Self, String> {
+ pub fn new(precision: u32) -> Result<Self, Error> {
Self::with_nullable(true, precision)
}
- pub fn with_nullable(nullable: bool, precision: u32) -> Result<Self,
String> {
+ pub fn with_nullable(nullable: bool, precision: u32) -> Result<Self,
Error> {
if !(Self::MIN_PRECISION..=Self::MAX_PRECISION).contains(&precision) {
- return Err(format!(
- "Time precision must be between {} and {} (both inclusive).",
- Self::MIN_PRECISION,
- Self::MAX_PRECISION
- ));
+ return DataTypeInvalidSnafu {
+ message: format!(
+ "Time precision must be between {} and {} (both
inclusive).",
+ Self::MIN_PRECISION,
+ Self::MAX_PRECISION
+ ),
+ }
+ .fail();
}
Ok(TimeType {
@@ -840,17 +858,20 @@ impl TimestampType {
pub const DEFAULT_PRECISION: u32 = 6;
- pub fn new(precision: u32) -> Result<Self, String> {
+ pub fn new(precision: u32) -> Result<Self, Error> {
Self::with_nullable(true, precision)
}
- pub fn with_nullable(nullable: bool, precision: u32) -> Result<Self,
String> {
+ pub fn with_nullable(nullable: bool, precision: u32) -> Result<Self,
Error> {
if !(Self::MIN_PRECISION..=Self::MAX_PRECISION).contains(&precision) {
- return Err(format!(
- "Timestamp precision must be between {} and {} (both
inclusive).",
- Self::MIN_PRECISION,
- Self::MAX_PRECISION
- ));
+ return DataTypeInvalidSnafu {
+ message: format!(
+ "Timestamp precision must be between {} and {} (both
inclusive).",
+ Self::MIN_PRECISION,
+ Self::MAX_PRECISION
+ ),
+ }
+ .fail();
}
Ok(TimestampType {
@@ -945,13 +966,16 @@ impl VarBinaryType {
pub const DEFAULT_LENGTH: u32 = 1;
- pub fn new(length: u32) -> Result<Self, String> {
+ pub fn new(length: u32) -> Result<Self, Error> {
Self::try_new(true, length)
}
- pub fn try_new(nullable: bool, length: u32) -> Result<Self, String> {
+ pub fn try_new(nullable: bool, length: u32) -> Result<Self, Error> {
if length < Self::MIN_LENGTH {
- return Err("Binary string length must be at least 1.".to_string());
+ return DataTypeInvalidSnafu {
+ message: "VarBinary string length must be at least
1.".to_string(),
+ }
+ .fail();
}
Ok(VarBinaryType { nullable, length })
@@ -1000,17 +1024,20 @@ impl VarCharType {
pub const DEFAULT_LENGTH: u32 = 1;
- pub fn new(length: u32) -> Result<Self, String> {
+ pub fn new(length: u32) -> Result<Self, Error> {
Self::with_nullable(true, length)
}
- pub fn with_nullable(nullable: bool, length: u32) -> Result<Self, String> {
+ pub fn with_nullable(nullable: bool, length: u32) -> Result<Self, Error> {
if !(Self::MIN_LENGTH..=Self::MAX_LENGTH).contains(&length) {
- return Err(format!(
- "Character string length must be between {} and {} (both
inclusive).",
- Self::MIN_LENGTH,
- Self::MAX_LENGTH
- ));
+ return DataTypeInvalidSnafu {
+ message: format!(
+ "VarChar string length must be between {} and {} (both
inclusive).",
+ Self::MIN_LENGTH,
+ Self::MAX_LENGTH
+ ),
+ }
+ .fail();
}
Ok(VarCharType { nullable, length })