(unsigned_number: &str, negative: bool)
| 359 | } |
| 360 | |
| 361 | fn parse_decimal(unsigned_number: &str, negative: bool) -> Result<Expr> { |
| 362 | let mut dec = BigDecimal::from_str(unsigned_number).map_err(|e| { |
| 363 | DataFusionError::from(ParserError(format!( |
| 364 | "Cannot parse {unsigned_number} as BigDecimal: {e}" |
| 365 | ))) |
| 366 | })?; |
| 367 | if negative { |
| 368 | dec = dec.neg(); |
| 369 | } |
| 370 | |
| 371 | let digits = dec.digits(); |
| 372 | let (int_val, scale) = dec.into_bigint_and_exponent(); |
| 373 | if scale < i8::MIN as i64 { |
| 374 | return not_impl_err!( |
| 375 | "Decimal scale {} exceeds the minimum supported scale: {}", |
| 376 | scale, |
| 377 | i8::MIN |
| 378 | ); |
| 379 | } |
| 380 | let precision = if scale > 0 { |
| 381 | // arrow-rs requires the precision to include the positive scale. |
| 382 | // See <https://github.com/apache/arrow-rs/blob/123045cc766d42d1eb06ee8bb3f09e39ea995ddc/arrow-array/src/types.rs#L1230> |
| 383 | std::cmp::max(digits, scale.unsigned_abs()) |
| 384 | } else { |
| 385 | digits |
| 386 | }; |
| 387 | if precision <= DECIMAL128_MAX_PRECISION as u64 { |
| 388 | let val = int_val.to_i128().ok_or_else(|| { |
| 389 | // Failures are unexpected here as we have already checked the precision |
| 390 | internal_datafusion_err!( |
| 391 | "Unexpected overflow when converting {} to i128", |
| 392 | int_val |
| 393 | ) |
| 394 | })?; |
| 395 | Ok(Expr::Literal( |
| 396 | ScalarValue::Decimal128(Some(val), precision as u8, scale as i8), |
| 397 | None, |
| 398 | )) |
| 399 | } else if precision <= DECIMAL256_MAX_PRECISION as u64 { |
| 400 | let val = bigint_to_i256(&int_val).ok_or_else(|| { |
| 401 | // Failures are unexpected here as we have already checked the precision |
| 402 | internal_datafusion_err!( |
| 403 | "Unexpected overflow when converting {} to i256", |
| 404 | int_val |
| 405 | ) |
| 406 | })?; |
| 407 | Ok(Expr::Literal( |
| 408 | ScalarValue::Decimal256(Some(val), precision as u8, scale as i8), |
| 409 | None, |
| 410 | )) |
| 411 | } else { |
| 412 | not_impl_err!( |
| 413 | "Decimal precision {} exceeds the maximum supported precision: {}", |
| 414 | precision, |
| 415 | DECIMAL256_MAX_PRECISION |
| 416 | ) |
| 417 | } |
| 418 | } |
searching dependent graphs…