Skip to content

Commit 1dd6dab

Browse files
committed
Address PR Comments
1 parent 9df2a11 commit 1dd6dab

5 files changed

Lines changed: 1004 additions & 884 deletions

File tree

arrow-avro/src/codec.rs

Lines changed: 111 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,27 +357,57 @@ impl AvroDataType {
357357
| Codec::DurationMillis
358358
| Codec::DurationSeconds => AvroLiteral::Long(parse_json_i64(default_json, "long")?),
359359
#[cfg(feature = "avro_custom_types")]
360-
Codec::Int8 | Codec::Int16 => {
360+
Codec::Int8 => {
361361
let i = parse_json_i64(default_json, "int")?;
362-
if i < i32::MIN as i64 || i > i32::MAX as i64 {
362+
if i < i8::MIN as i64 || i > i8::MAX as i64 {
363363
return Err(ArrowError::SchemaError(format!(
364-
"Default int {i} out of i32 range"
364+
"Default int8 {i} out of i8 range"
365365
)));
366366
}
367367
AvroLiteral::Int(i as i32)
368368
}
369369
#[cfg(feature = "avro_custom_types")]
370-
Codec::UInt8 | Codec::UInt16 => {
370+
Codec::Int16 => {
371371
let i = parse_json_i64(default_json, "int")?;
372-
if i < 0 || i > i32::MAX as i64 {
372+
if i < i16::MIN as i64 || i > i16::MAX as i64 {
373373
return Err(ArrowError::SchemaError(format!(
374-
"Default unsigned int {i} out of range"
374+
"Default int16 {i} out of i16 range"
375375
)));
376376
}
377377
AvroLiteral::Int(i as i32)
378378
}
379379
#[cfg(feature = "avro_custom_types")]
380-
Codec::UInt32 | Codec::Date64 | Codec::TimeNanos | Codec::TimestampSecs(_) => {
380+
Codec::UInt8 => {
381+
let i = parse_json_i64(default_json, "int")?;
382+
if i < 0 || i > u8::MAX as i64 {
383+
return Err(ArrowError::SchemaError(format!(
384+
"Default uint8 {i} out of u8 range"
385+
)));
386+
}
387+
AvroLiteral::Int(i as i32)
388+
}
389+
#[cfg(feature = "avro_custom_types")]
390+
Codec::UInt16 => {
391+
let i = parse_json_i64(default_json, "int")?;
392+
if i < 0 || i > u16::MAX as i64 {
393+
return Err(ArrowError::SchemaError(format!(
394+
"Default uint16 {i} out of u16 range"
395+
)));
396+
}
397+
AvroLiteral::Int(i as i32)
398+
}
399+
#[cfg(feature = "avro_custom_types")]
400+
Codec::UInt32 => {
401+
let i = parse_json_i64(default_json, "long")?;
402+
if i < 0 || i > u32::MAX as i64 {
403+
return Err(ArrowError::SchemaError(format!(
404+
"Default uint32 {i} out of u32 range"
405+
)));
406+
}
407+
AvroLiteral::Long(i)
408+
}
409+
#[cfg(feature = "avro_custom_types")]
410+
Codec::Date64 | Codec::TimeNanos | Codec::TimestampSecs(_) => {
381411
AvroLiteral::Long(parse_json_i64(default_json, "long")?)
382412
}
383413
#[cfg(feature = "avro_custom_types")]
@@ -3017,6 +3047,80 @@ mod tests {
30173047
assert_eq!(l2, AvroLiteral::Long(456));
30183048
}
30193049

3050+
#[cfg(feature = "avro_custom_types")]
3051+
#[test]
3052+
fn test_validate_and_store_default_custom_integer_ranges() {
3053+
let mut dt_i8 = AvroDataType::new(Codec::Int8, HashMap::new(), None);
3054+
let lit_i8 = dt_i8
3055+
.parse_and_store_default(&serde_json::json!(i8::MAX))
3056+
.unwrap();
3057+
assert_eq!(lit_i8, AvroLiteral::Int(i8::MAX as i32));
3058+
let err_i8_high = dt_i8
3059+
.parse_and_store_default(&serde_json::json!(i8::MAX as i64 + 1))
3060+
.unwrap_err();
3061+
assert!(err_i8_high.to_string().contains("out of i8 range"));
3062+
let err_i8_low = dt_i8
3063+
.parse_and_store_default(&serde_json::json!(i8::MIN as i64 - 1))
3064+
.unwrap_err();
3065+
assert!(err_i8_low.to_string().contains("out of i8 range"));
3066+
3067+
let mut dt_i16 = AvroDataType::new(Codec::Int16, HashMap::new(), None);
3068+
let lit_i16 = dt_i16
3069+
.parse_and_store_default(&serde_json::json!(i16::MIN))
3070+
.unwrap();
3071+
assert_eq!(lit_i16, AvroLiteral::Int(i16::MIN as i32));
3072+
let err_i16_high = dt_i16
3073+
.parse_and_store_default(&serde_json::json!(i16::MAX as i64 + 1))
3074+
.unwrap_err();
3075+
assert!(err_i16_high.to_string().contains("out of i16 range"));
3076+
let err_i16_low = dt_i16
3077+
.parse_and_store_default(&serde_json::json!(i16::MIN as i64 - 1))
3078+
.unwrap_err();
3079+
assert!(err_i16_low.to_string().contains("out of i16 range"));
3080+
3081+
let mut dt_u8 = AvroDataType::new(Codec::UInt8, HashMap::new(), None);
3082+
let lit_u8 = dt_u8
3083+
.parse_and_store_default(&serde_json::json!(u8::MAX))
3084+
.unwrap();
3085+
assert_eq!(lit_u8, AvroLiteral::Int(u8::MAX as i32));
3086+
let err_u8_neg = dt_u8
3087+
.parse_and_store_default(&serde_json::json!(-1))
3088+
.unwrap_err();
3089+
assert!(err_u8_neg.to_string().contains("out of u8 range"));
3090+
let err_u8_high = dt_u8
3091+
.parse_and_store_default(&serde_json::json!(u8::MAX as i64 + 1))
3092+
.unwrap_err();
3093+
assert!(err_u8_high.to_string().contains("out of u8 range"));
3094+
3095+
let mut dt_u16 = AvroDataType::new(Codec::UInt16, HashMap::new(), None);
3096+
let lit_u16 = dt_u16
3097+
.parse_and_store_default(&serde_json::json!(u16::MAX))
3098+
.unwrap();
3099+
assert_eq!(lit_u16, AvroLiteral::Int(u16::MAX as i32));
3100+
let err_u16_neg = dt_u16
3101+
.parse_and_store_default(&serde_json::json!(-1))
3102+
.unwrap_err();
3103+
assert!(err_u16_neg.to_string().contains("out of u16 range"));
3104+
let err_u16_high = dt_u16
3105+
.parse_and_store_default(&serde_json::json!(u16::MAX as i64 + 1))
3106+
.unwrap_err();
3107+
assert!(err_u16_high.to_string().contains("out of u16 range"));
3108+
3109+
let mut dt_u32 = AvroDataType::new(Codec::UInt32, HashMap::new(), None);
3110+
let lit_u32 = dt_u32
3111+
.parse_and_store_default(&serde_json::json!(u32::MAX as i64))
3112+
.unwrap();
3113+
assert_eq!(lit_u32, AvroLiteral::Long(u32::MAX as i64));
3114+
let err_u32_neg = dt_u32
3115+
.parse_and_store_default(&serde_json::json!(-1))
3116+
.unwrap_err();
3117+
assert!(err_u32_neg.to_string().contains("out of u32 range"));
3118+
let err_u32_high = dt_u32
3119+
.parse_and_store_default(&serde_json::json!(u32::MAX as i64 + 1))
3120+
.unwrap_err();
3121+
assert!(err_u32_high.to_string().contains("out of u32 range"));
3122+
}
3123+
30203124
#[test]
30213125
fn test_validate_and_store_default_fixed_decimal_interval() {
30223126
let mut dt_fixed = AvroDataType::new(Codec::Fixed(4), HashMap::new(), None);

0 commit comments

Comments
 (0)