MCPcopy Create free account
hub / github.com/apache/datafusion / parse_decimal

Function parse_decimal

datafusion/sql/src/expr/value.rs:361–418  ·  view source on GitHub ↗
(unsigned_number: &str, negative: bool)

Source from the content-addressed store, hash-verified

359}
360
361fn 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}

Callers 2

parse_sql_numberMethod · 0.85
test_parse_decimalFunction · 0.85

Calls 7

from_strFunction · 0.85
maxFunction · 0.85
bigint_to_i256Function · 0.85
LiteralInterface · 0.50
Decimal128Class · 0.50
Decimal256Class · 0.50
negMethod · 0.45

Tested by 1

test_parse_decimalFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…