Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
andygrove committed Apr 25, 2024
1 parent 4f2539d commit de87b5d
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions core/src/execution/datafusion/expressions/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,21 +324,25 @@ impl CastStringToInt for CastStringToInt32 {
str: &str,
digit: u32,
) -> CometResult<()> {
if self.result.is_some() && self.result.unwrap() < i32::MIN / self.radix {
self.reset();
return none_or_err(eval_mode, type_name, str);
// We are going to process the new digit and accumulate the result. However, before doing
// this, if the result is already smaller than the stopValue(Integer.MIN_VALUE / radix),
// then result * 10 will definitely be smaller than minValue, and we can stop
if let Some(r) = self.result {
let stop_value = i32::MIN / self.radix;
if r < stop_value {
self.reset();
return none_or_err(eval_mode, type_name, str);
}
}
// Since the previous result is less than or equal to stopValue(Integer.MIN_VALUE / radix),
// we can just use `result > 0` to check overflow. If result overflows, we should stop
let v = self.result.unwrap_or(0) * self.radix;
if let Some(x) = v.checked_sub(digit as i32) {
if x > 0 {
match v.checked_sub(digit as i32) {
Some(x) if x <= 0 => self.result = Some(x),
_ => {
self.reset();
return none_or_err(eval_mode, type_name, str);
} else {
self.result = Some(x);
}
} else {
self.reset();
return none_or_err(eval_mode, type_name, str);
}
Ok(())
}
Expand All @@ -357,6 +361,7 @@ impl CastStringToInt for CastStringToInt32 {
if let Some(r) = self.result {
let negated = r.checked_neg().unwrap_or(-1);
if negated < 0 {
self.reset();
return none_or_err(eval_mode, type_name, str);
}
self.result = Some(negated);
Expand Down Expand Up @@ -390,21 +395,25 @@ impl CastStringToInt for CastStringToInt64 {
str: &str,
digit: u32,
) -> CometResult<()> {
if self.result.unwrap_or(0) < i64::MIN / self.radix {
self.reset();
return none_or_err(eval_mode, type_name, str);
// We are going to process the new digit and accumulate the result. However, before doing
// this, if the result is already smaller than the stopValue(Integer.MIN_VALUE / radix),
// then result * 10 will definitely be smaller than minValue, and we can stop
if let Some(r) = self.result {
let stop_value = i64::MIN / self.radix;
if r < stop_value {
self.reset();
return none_or_err(eval_mode, type_name, str);
}
}
// Since the previous result is less than or equal to stopValue(Integer.MIN_VALUE / radix),
// we can just use `result > 0` to check overflow. If result overflows, we should stop
let v = self.result.unwrap_or(0) * self.radix;
if let Some(x) = v.checked_sub(digit as i64) {
if x > 0 {
match v.checked_sub(digit as i64) {
Some(x) if x <= 0 => self.result = Some(x),
_ => {
self.reset();
return none_or_err(eval_mode, type_name, str);
} else {
self.result = Some(x);
}
} else {
self.reset();
return none_or_err(eval_mode, type_name, str);
}
Ok(())
}
Expand All @@ -424,6 +433,7 @@ impl CastStringToInt for CastStringToInt64 {
if let Some(r) = self.result {
let negated = r.checked_neg().unwrap_or(-1);
if negated < 0 {
self.reset();
return none_or_err(eval_mode, type_name, str);
}
self.result = Some(negated);
Expand Down

0 comments on commit de87b5d

Please sign in to comment.