Update bigint/bigdecimal (#2585)

* Update bigint/bigdecimal

* clippy
This commit is contained in:
Jonathan Turner
2020-09-22 05:28:31 +12:00
committed by GitHub
parent 7a595827f1
commit 9b577b8679
35 changed files with 263 additions and 132 deletions

View File

@ -1099,8 +1099,18 @@ impl Expression {
Expression::Literal(Literal::Number(Number::Int(BigInt::from(i))))
}
pub fn decimal(f: f64) -> Expression {
Expression::Literal(Literal::Number(Number::Decimal(BigDecimal::from(f))))
pub fn decimal(f: f64) -> Result<Expression, ParseError> {
let dec = BigDecimal::from_f64(f);
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 {

View File

@ -17,6 +17,7 @@ use crate::value::primitive::Primitive;
use crate::value::range::{Range, RangeInclusion};
use crate::{ColumnPath, PathMember, UnspannedPathMember};
use bigdecimal::BigDecimal;
use bigdecimal::FromPrimitive;
use chrono::{DateTime, Utc};
use indexmap::IndexMap;
use itertools::Itertools;
@ -190,8 +191,22 @@ impl UntaggedValue {
}
/// Helper for creating decimal values
pub fn decimal(s: impl Into<BigDecimal>) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::Decimal(s.into()))
pub fn decimal(s: BigDecimal) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::Decimal(s))
}
/// Helper for creating decimal values
pub fn decimal_from_float(f: f64, span: Span) -> UntaggedValue {
let dec = BigDecimal::from_f64(f);
match dec {
Some(dec) => UntaggedValue::Primitive(Primitive::Decimal(dec)),
None => UntaggedValue::Error(ShellError::labeled_error(
"Can not convert f64 to big decimal",
"can not create decimal",
span,
)),
}
}
/// Helper for creating binary (non-text) buffer values