Parse decimals as BigDecimal (#2644)

Use implicit serde from BigDecimal crate
This commit is contained in:
Chris Gillespie 2020-10-06 18:01:40 -07:00 committed by GitHub
parent f14f4e39c5
commit 54326869e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 58 deletions

View File

@ -15,6 +15,7 @@ use num_bigint::BigInt;
use crate::lite_parse::{lite_parse, LiteBlock, LiteCommand, LitePipeline}; use crate::lite_parse::{lite_parse, LiteBlock, LiteCommand, LitePipeline};
use crate::path::expand_path; use crate::path::expand_path;
use crate::signature::SignatureRegistry; use crate::signature::SignatureRegistry;
use bigdecimal::BigDecimal;
/// Parses a simple column path, one without a variable (implied or explicit) at the head /// Parses a simple column path, one without a variable (implied or explicit) at the head
fn parse_simple_column_path(lite_arg: &Spanned<String>) -> (SpannedExpression, Option<ParseError>) { fn parse_simple_column_path(lite_arg: &Spanned<String>) -> (SpannedExpression, Option<ParseError>) {
@ -740,15 +741,11 @@ fn parse_arg(
SpannedExpression::new(Expression::integer(x), lite_arg.span), SpannedExpression::new(Expression::integer(x), lite_arg.span),
None, None,
) )
} else if let Ok(x) = lite_arg.item.parse::<f64>() { } else if let Ok(x) = lite_arg.item.parse::<BigDecimal>() {
if let Ok(x) = Expression::decimal(x) {
(SpannedExpression::new(x, lite_arg.span), None)
} else {
( (
garbage(lite_arg.span), SpannedExpression::new(Expression::decimal(x), lite_arg.span),
Some(ParseError::mismatch("number", lite_arg.clone())), None,
) )
}
} else { } else {
( (
garbage(lite_arg.span), garbage(lite_arg.span),
@ -765,7 +762,7 @@ fn parse_arg(
} else { } else {
( (
garbage(lite_arg.span), garbage(lite_arg.span),
Some(ParseError::mismatch("number", lite_arg.clone())), Some(ParseError::mismatch("int", lite_arg.clone())),
) )
} }
} }
@ -951,43 +948,59 @@ mod test {
#[test] #[test]
fn parse_number() -> Result<(), ParseError> { fn parse_number() -> Result<(), ParseError> {
let registry = MockRegistry::new();
let raw = "-32.2".to_string(); let raw = "-32.2".to_string();
let input = raw.clone().spanned(Span::new(0, raw.len())); let input = raw.clone().spanned(Span::new(0, raw.len()));
let registry = MockRegistry::new();
let result = parse_arg(SyntaxShape::Number, &registry, &input); let result = parse_arg(SyntaxShape::Number, &registry, &input);
assert_eq!(result.1, None); assert_eq!(result.1, None);
assert_eq!(result.0.expr, Expression::decimal(-32.2)?); assert_eq!(
result.0.expr,
Expression::decimal(BigDecimal::new(BigInt::from(-322), 1))
);
let raw = "32.2".to_string(); let raw = "32.2".to_string();
let input = raw.clone().spanned(Span::new(0, raw.len())); let input = raw.clone().spanned(Span::new(0, raw.len()));
let registry = MockRegistry::new();
let result = parse_arg(SyntaxShape::Number, &registry, &input); let result = parse_arg(SyntaxShape::Number, &registry, &input);
assert_eq!(result.1, None); assert_eq!(result.1, None);
assert_eq!(result.0.expr, Expression::decimal(32.2)?); assert_eq!(
result.0.expr,
Expression::decimal(BigDecimal::new(BigInt::from(322), 1))
);
let raw = "36893488147419103232.54".to_string();
let input = raw.clone().spanned(Span::new(0, raw.len()));
let result = parse_arg(SyntaxShape::Number, &registry, &input);
assert_eq!(result.1, None);
assert_eq!(
result.0.expr,
Expression::decimal(BigDecimal::new(
BigInt::from(3689348814741910323254 as i128),
2
))
);
let raw = "-34".to_string(); let raw = "-34".to_string();
let input = raw.clone().spanned(Span::new(0, raw.len())); let input = raw.clone().spanned(Span::new(0, raw.len()));
let registry = MockRegistry::new();
let result = parse_arg(SyntaxShape::Number, &registry, &input); let result = parse_arg(SyntaxShape::Number, &registry, &input);
assert_eq!(result.1, None); assert_eq!(result.1, None);
assert_eq!(result.0.expr, Expression::integer(BigInt::from(-34))); assert_eq!(result.0.expr, Expression::integer(BigInt::from(-34)));
let raw = "34".to_string(); let raw = "34".to_string();
let input = raw.clone().spanned(Span::new(0, raw.len())); let input = raw.clone().spanned(Span::new(0, raw.len()));
let registry = MockRegistry::new();
let result = parse_arg(SyntaxShape::Number, &registry, &input); let result = parse_arg(SyntaxShape::Number, &registry, &input);
assert_eq!(result.1, None); assert_eq!(result.1, None);
assert_eq!(result.0.expr, Expression::integer(BigInt::from(34))); assert_eq!(result.0.expr, Expression::integer(BigInt::from(34)));
let raw = "36893488147419103232".to_string(); let raw = "36893488147419103232".to_string();
let input = raw.clone().spanned(Span::new(0, raw.len())); let input = raw.clone().spanned(Span::new(0, raw.len()));
let registry = MockRegistry::new();
let result = parse_arg(SyntaxShape::Number, &registry, &input); let result = parse_arg(SyntaxShape::Number, &registry, &input);
assert_eq!(result.1, None); assert_eq!(result.1, None);
assert_eq!( assert_eq!(
result.0.expr, result.0.expr,
Expression::integer(BigInt::from(36893488147419103232 as u128)) Expression::integer(BigInt::from(36893488147419103232 as u128))
); );
Ok(()) Ok(())
} }
} }

View File

@ -1099,18 +1099,8 @@ impl Expression {
Expression::Literal(Literal::Number(Number::Int(i))) Expression::Literal(Literal::Number(Number::Int(i)))
} }
pub fn decimal(f: f64) -> Result<Expression, ParseError> { pub fn decimal(dec: BigDecimal) -> Expression {
let dec = BigDecimal::from_f64(f); Expression::Literal(Literal::Number(Number::Decimal(dec)))
let dec = match dec {
Some(x) => Ok(x),
None => Err(ParseError::internal_error(
"Can not convert f64 to big decimal"
.to_string()
.spanned_unknown(),
)),
}?;
Ok(Expression::Literal(Literal::Number(Number::Decimal(dec))))
} }
pub fn string(s: String) -> Expression { pub fn string(s: String) -> Expression {

View File

@ -7,7 +7,6 @@ pub mod evaluate;
pub mod iter; pub mod iter;
pub mod primitive; pub mod primitive;
pub mod range; pub mod range;
mod serde_bigdecimal;
use crate::hir; use crate::hir;
use crate::type_name::{ShellTypeName, SpannedTypeName}; use crate::type_name::{ShellTypeName, SpannedTypeName};

View File

@ -1,7 +1,6 @@
use crate::type_name::ShellTypeName; use crate::type_name::ShellTypeName;
use crate::value::column_path::ColumnPath; use crate::value::column_path::ColumnPath;
use crate::value::range::{Range, RangeInclusion}; use crate::value::range::{Range, RangeInclusion};
use crate::value::serde_bigdecimal;
use bigdecimal::BigDecimal; use bigdecimal::BigDecimal;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use nu_errors::{ExpectedRange, ShellError}; use nu_errors::{ExpectedRange, ShellError};
@ -27,7 +26,6 @@ pub enum Primitive {
/// A "big int", an integer with arbitrarily large size (aka not limited to 64-bit) /// A "big int", an integer with arbitrarily large size (aka not limited to 64-bit)
Int(BigInt), Int(BigInt),
/// A "big decimal", an decimal number with arbitrarily large size (aka not limited to 64-bit) /// A "big decimal", an decimal number with arbitrarily large size (aka not limited to 64-bit)
#[serde(with = "serde_bigdecimal")]
Decimal(BigDecimal), Decimal(BigDecimal),
/// A count in the number of bytes, used as a filesize /// A count in the number of bytes, used as a filesize
Filesize(u64), Filesize(u64),

View File

@ -1,26 +0,0 @@
use bigdecimal::BigDecimal;
use num_traits::cast::FromPrimitive;
use num_traits::cast::ToPrimitive;
/// Enable big decimal serialization by providing a `serialize` function
pub fn serialize<S>(big_decimal: &BigDecimal, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serde::Serialize::serialize(
&big_decimal
.to_f64()
.ok_or_else(|| serde::ser::Error::custom("expected a f64-sized bignum"))?,
serializer,
)
}
/// Enable big decimal deserialization by providing a `deserialize` function
pub fn deserialize<'de, D>(deserializer: D) -> Result<BigDecimal, D::Error>
where
D: serde::Deserializer<'de>,
{
let x: f64 = serde::Deserialize::deserialize(deserializer)?;
Ok(BigDecimal::from_f64(x)
.ok_or_else(|| serde::de::Error::custom("expected a f64-sized bigdecimal"))?)
}