Skip to content

Commit

Permalink
refactor: Refactor error in DataType (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
XuQianJin-Stars authored Aug 4, 2024
1 parent 7302564 commit e86c828
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 50 deletions.
5 changes: 5 additions & 0 deletions crates/paimon/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
127 changes: 77 additions & 50 deletions crates/paimon/src/spec/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 })
}
Expand Down Expand Up @@ -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 })
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 })
Expand Down Expand Up @@ -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 })
Expand Down

0 comments on commit e86c828

Please sign in to comment.