Drops the dependency on num_traits

pull/22/head
Tpt 5 years ago
parent c49da613ea
commit 603b58c856
  1. 1
      lib/Cargo.toml
  2. 6
      lib/src/model/xsd/decimal.rs
  3. 65
      lib/src/sparql/eval.rs

@ -17,7 +17,6 @@ lazy_static = "1"
rocksdb = { version = "0.13", optional = true } rocksdb = { version = "0.13", optional = true }
byteorder = { version = "1", features = ["i128"] } byteorder = { version = "1", features = ["i128"] }
quick-xml = "0.17" quick-xml = "0.17"
num-traits = "0.2"
rand = "0.7" rand = "0.7"
md-5 = "0.8" md-5 = "0.8"
sha-1 = "0.8" sha-1 = "0.8"

@ -458,15 +458,13 @@ impl Decimal {
/// Creates a `Decimal` from a `f32` without taking care of precision /// Creates a `Decimal` from a `f32` without taking care of precision
#[inline] #[inline]
pub(crate) fn from_f32(v: f32) -> Self { pub(crate) fn from_f32(v: f32) -> Self {
Self { Self::from_f64(v.into())
value: (v * (DECIMAL_PART_POW as f32)) as i128,
}
} }
/// Creates a `f32` from a `Decimal` without taking care of precision /// Creates a `f32` from a `Decimal` without taking care of precision
#[inline] #[inline]
pub fn to_f32(&self) -> f32 { pub fn to_f32(&self) -> f32 {
(self.value as f32) / (DECIMAL_PART_POW as f32) self.to_f64() as f32
} }
/// Creates a `Decimal` from a `f64` without taking care of precision /// Creates a `Decimal` from a `f64` without taking care of precision

@ -11,7 +11,6 @@ use crate::Result;
use digest::Digest; use digest::Digest;
use failure::format_err; use failure::format_err;
use md5::Md5; use md5::Md5;
use num_traits::ToPrimitive;
use rand::random; use rand::random;
use regex::{Regex, RegexBuilder}; use regex::{Regex, RegexBuilder};
use rio_api::iri::Iri; use rio_api::iri::Iri;
@ -1291,35 +1290,31 @@ impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> {
_ => None, _ => None,
}, },
PlanExpression::DoubleCast(e) => match self.eval_expression(e, tuple)? { PlanExpression::DoubleCast(e) => match self.eval_expression(e, tuple)? {
EncodedTerm::FloatLiteral(value) => Some(value.to_f64()?.into()), EncodedTerm::FloatLiteral(value) => Some(f64::from(value).into()),
EncodedTerm::DoubleLiteral(value) => Some(value.to_f64()?.into()), EncodedTerm::DoubleLiteral(value) => Some(value.into()),
EncodedTerm::IntegerLiteral(value) => Some(value.to_f64()?.into()), EncodedTerm::IntegerLiteral(value) => Some((value as f64).into()),
EncodedTerm::DecimalLiteral(value) => Some(value.to_f64().into()), EncodedTerm::DecimalLiteral(value) => Some(value.to_f64().into()),
EncodedTerm::BooleanLiteral(value) => { EncodedTerm::BooleanLiteral(value) => Some(if value { 1f64 } else { 0f64 }.into()),
Some(if value { 1. as f64 } else { 0. }.into())
}
EncodedTerm::StringLiteral { value_id } => { EncodedTerm::StringLiteral { value_id } => {
parse_double_str(&*self.dataset.get_str(value_id).ok()??) parse_double_str(&*self.dataset.get_str(value_id).ok()??)
} }
_ => None, _ => None,
}, },
PlanExpression::FloatCast(e) => match self.eval_expression(e, tuple)? { PlanExpression::FloatCast(e) => match self.eval_expression(e, tuple)? {
EncodedTerm::FloatLiteral(value) => Some(value.to_f32()?.into()), EncodedTerm::FloatLiteral(value) => Some(value.into()),
EncodedTerm::DoubleLiteral(value) => Some(value.to_f32()?.into()), EncodedTerm::DoubleLiteral(value) => Some((value as f32).into()),
EncodedTerm::IntegerLiteral(value) => Some(value.to_f32()?.into()), EncodedTerm::IntegerLiteral(value) => Some((value as f32).into()),
EncodedTerm::DecimalLiteral(value) => Some(value.to_f32().into()), EncodedTerm::DecimalLiteral(value) => Some(value.to_f32().into()),
EncodedTerm::BooleanLiteral(value) => { EncodedTerm::BooleanLiteral(value) => Some(if value { 1f32 } else { 0f32 }.into()),
Some(if value { 1. as f32 } else { 0. }.into())
}
EncodedTerm::StringLiteral { value_id } => { EncodedTerm::StringLiteral { value_id } => {
parse_float_str(&*self.dataset.get_str(value_id).ok()??) parse_float_str(&*self.dataset.get_str(value_id).ok()??)
} }
_ => None, _ => None,
}, },
PlanExpression::IntegerCast(e) => match self.eval_expression(e, tuple)? { PlanExpression::IntegerCast(e) => match self.eval_expression(e, tuple)? {
EncodedTerm::FloatLiteral(value) => Some(value.to_i64()?.into()), EncodedTerm::FloatLiteral(value) => Some((value as i64).into()),
EncodedTerm::DoubleLiteral(value) => Some(value.to_i64()?.into()), EncodedTerm::DoubleLiteral(value) => Some((value as i64).into()),
EncodedTerm::IntegerLiteral(value) => Some(value.to_i64()?.into()), EncodedTerm::IntegerLiteral(value) => Some(value.into()),
EncodedTerm::DecimalLiteral(value) => Some(i64::try_from(value).ok()?.into()), EncodedTerm::DecimalLiteral(value) => Some(i64::try_from(value).ok()?.into()),
EncodedTerm::BooleanLiteral(value) => Some(if value { 1 } else { 0 }.into()), EncodedTerm::BooleanLiteral(value) => Some(if value { 1 } else { 0 }.into()),
EncodedTerm::StringLiteral { value_id } => { EncodedTerm::StringLiteral { value_id } => {
@ -1620,23 +1615,23 @@ impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> {
}, },
EncodedTerm::FloatLiteral(a) => match b { EncodedTerm::FloatLiteral(a) => match b {
EncodedTerm::FloatLiteral(b) => Some(a == b), EncodedTerm::FloatLiteral(b) => Some(a == b),
EncodedTerm::DoubleLiteral(b) => Some(a.to_f64()? == b), EncodedTerm::DoubleLiteral(b) => Some(f64::from(a) == b),
EncodedTerm::IntegerLiteral(b) => Some(a == b.to_f32()?), EncodedTerm::IntegerLiteral(b) => Some(a == b as f32),
EncodedTerm::DecimalLiteral(b) => Some(a == b.to_f32()), EncodedTerm::DecimalLiteral(b) => Some(a == b.to_f32()),
EncodedTerm::TypedLiteral { .. } => None, EncodedTerm::TypedLiteral { .. } => None,
_ => Some(false), _ => Some(false),
}, },
EncodedTerm::DoubleLiteral(a) => match b { EncodedTerm::DoubleLiteral(a) => match b {
EncodedTerm::FloatLiteral(b) => Some(a == b.to_f64()?), EncodedTerm::FloatLiteral(b) => Some(a == f64::from(b)),
EncodedTerm::DoubleLiteral(b) => Some(a == b), EncodedTerm::DoubleLiteral(b) => Some(a == b),
EncodedTerm::IntegerLiteral(b) => Some(a == b.to_f64()?), EncodedTerm::IntegerLiteral(b) => Some(a == (b as f64)),
EncodedTerm::DecimalLiteral(b) => Some(a == b.to_f64()), EncodedTerm::DecimalLiteral(b) => Some(a == b.to_f64()),
EncodedTerm::TypedLiteral { .. } => None, EncodedTerm::TypedLiteral { .. } => None,
_ => Some(false), _ => Some(false),
}, },
EncodedTerm::IntegerLiteral(a) => match b { EncodedTerm::IntegerLiteral(a) => match b {
EncodedTerm::FloatLiteral(b) => Some(a.to_f32()? == b), EncodedTerm::FloatLiteral(b) => Some((a as f32) == b),
EncodedTerm::DoubleLiteral(b) => Some(a.to_f64()? == b), EncodedTerm::DoubleLiteral(b) => Some((a as f64) == b),
EncodedTerm::IntegerLiteral(b) => Some(a == b), EncodedTerm::IntegerLiteral(b) => Some(a == b),
EncodedTerm::DecimalLiteral(b) => Some(Decimal::from(a) == b), EncodedTerm::DecimalLiteral(b) => Some(Decimal::from(a) == b),
EncodedTerm::TypedLiteral { .. } => None, EncodedTerm::TypedLiteral { .. } => None,
@ -1733,21 +1728,21 @@ impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> {
} }
EncodedTerm::FloatLiteral(a) => match b { EncodedTerm::FloatLiteral(a) => match b {
EncodedTerm::FloatLiteral(ref b) => a.partial_cmp(b), EncodedTerm::FloatLiteral(ref b) => a.partial_cmp(b),
EncodedTerm::DoubleLiteral(ref b) => a.to_f64()?.partial_cmp(b), EncodedTerm::DoubleLiteral(ref b) => f64::from(a).partial_cmp(b),
EncodedTerm::IntegerLiteral(b) => a.partial_cmp(&b.to_f32()?), EncodedTerm::IntegerLiteral(b) => a.partial_cmp(&(b as f32)),
EncodedTerm::DecimalLiteral(b) => a.partial_cmp(&b.to_f32()), EncodedTerm::DecimalLiteral(b) => a.partial_cmp(&b.to_f32()),
_ => None, _ => None,
}, },
EncodedTerm::DoubleLiteral(a) => match b { EncodedTerm::DoubleLiteral(a) => match b {
EncodedTerm::FloatLiteral(b) => a.partial_cmp(&b.to_f64()?), EncodedTerm::FloatLiteral(b) => a.partial_cmp(&b.into()),
EncodedTerm::DoubleLiteral(ref b) => a.partial_cmp(b), EncodedTerm::DoubleLiteral(ref b) => a.partial_cmp(b),
EncodedTerm::IntegerLiteral(b) => a.partial_cmp(&b.to_f64()?), EncodedTerm::IntegerLiteral(b) => a.partial_cmp(&(b as f64)),
EncodedTerm::DecimalLiteral(b) => a.partial_cmp(&b.to_f64()), EncodedTerm::DecimalLiteral(b) => a.partial_cmp(&b.to_f64()),
_ => None, _ => None,
}, },
EncodedTerm::IntegerLiteral(a) => match b { EncodedTerm::IntegerLiteral(a) => match b {
EncodedTerm::FloatLiteral(ref b) => a.to_f32()?.partial_cmp(b), EncodedTerm::FloatLiteral(ref b) => (a as f32).partial_cmp(b),
EncodedTerm::DoubleLiteral(ref b) => a.to_f64()?.partial_cmp(b), EncodedTerm::DoubleLiteral(ref b) => (a as f64).partial_cmp(b),
EncodedTerm::IntegerLiteral(ref b) => a.partial_cmp(b), EncodedTerm::IntegerLiteral(ref b) => a.partial_cmp(b),
EncodedTerm::DecimalLiteral(b) => Decimal::from(a).partial_cmp(&b), EncodedTerm::DecimalLiteral(b) => Decimal::from(a).partial_cmp(&b),
_ => None, _ => None,
@ -1829,34 +1824,34 @@ impl NumericBinaryOperands {
fn new(a: EncodedTerm, b: EncodedTerm) -> Option<Self> { fn new(a: EncodedTerm, b: EncodedTerm) -> Option<Self> {
match (a, b) { match (a, b) {
(EncodedTerm::FloatLiteral(v1), EncodedTerm::FloatLiteral(v2)) => { (EncodedTerm::FloatLiteral(v1), EncodedTerm::FloatLiteral(v2)) => {
Some(NumericBinaryOperands::Float(v1, v2.to_f32()?)) Some(NumericBinaryOperands::Float(v1, v2))
} }
(EncodedTerm::FloatLiteral(v1), EncodedTerm::DoubleLiteral(v2)) => { (EncodedTerm::FloatLiteral(v1), EncodedTerm::DoubleLiteral(v2)) => {
Some(NumericBinaryOperands::Double(v1.to_f64()?, v2)) Some(NumericBinaryOperands::Double(v1.into(), v2))
} }
(EncodedTerm::FloatLiteral(v1), EncodedTerm::IntegerLiteral(v2)) => { (EncodedTerm::FloatLiteral(v1), EncodedTerm::IntegerLiteral(v2)) => {
Some(NumericBinaryOperands::Float(v1, v2.to_f32()?)) Some(NumericBinaryOperands::Float(v1, v2 as f32))
} }
(EncodedTerm::FloatLiteral(v1), EncodedTerm::DecimalLiteral(v2)) => { (EncodedTerm::FloatLiteral(v1), EncodedTerm::DecimalLiteral(v2)) => {
Some(NumericBinaryOperands::Float(v1, v2.to_f32())) Some(NumericBinaryOperands::Float(v1, v2.to_f32()))
} }
(EncodedTerm::DoubleLiteral(v1), EncodedTerm::FloatLiteral(v2)) => { (EncodedTerm::DoubleLiteral(v1), EncodedTerm::FloatLiteral(v2)) => {
Some(NumericBinaryOperands::Double(v1, v2.to_f64()?)) Some(NumericBinaryOperands::Double(v1, v2.into()))
} }
(EncodedTerm::DoubleLiteral(v1), EncodedTerm::DoubleLiteral(v2)) => { (EncodedTerm::DoubleLiteral(v1), EncodedTerm::DoubleLiteral(v2)) => {
Some(NumericBinaryOperands::Double(v1, v2)) Some(NumericBinaryOperands::Double(v1, v2))
} }
(EncodedTerm::DoubleLiteral(v1), EncodedTerm::IntegerLiteral(v2)) => { (EncodedTerm::DoubleLiteral(v1), EncodedTerm::IntegerLiteral(v2)) => {
Some(NumericBinaryOperands::Double(v1, v2.to_f64()?)) Some(NumericBinaryOperands::Double(v1, v2 as f64))
} }
(EncodedTerm::DoubleLiteral(v1), EncodedTerm::DecimalLiteral(v2)) => { (EncodedTerm::DoubleLiteral(v1), EncodedTerm::DecimalLiteral(v2)) => {
Some(NumericBinaryOperands::Double(v1, v2.to_f64())) Some(NumericBinaryOperands::Double(v1, v2.to_f64()))
} }
(EncodedTerm::IntegerLiteral(v1), EncodedTerm::FloatLiteral(v2)) => { (EncodedTerm::IntegerLiteral(v1), EncodedTerm::FloatLiteral(v2)) => {
Some(NumericBinaryOperands::Float(v1.to_f32()?, v2)) Some(NumericBinaryOperands::Float(v1 as f32, v2))
} }
(EncodedTerm::IntegerLiteral(v1), EncodedTerm::DoubleLiteral(v2)) => { (EncodedTerm::IntegerLiteral(v1), EncodedTerm::DoubleLiteral(v2)) => {
Some(NumericBinaryOperands::Double(v1.to_f64()?, v2)) Some(NumericBinaryOperands::Double(v1 as f64, v2))
} }
(EncodedTerm::IntegerLiteral(v1), EncodedTerm::IntegerLiteral(v2)) => { (EncodedTerm::IntegerLiteral(v1), EncodedTerm::IntegerLiteral(v2)) => {
Some(NumericBinaryOperands::Integer(v1, v2)) Some(NumericBinaryOperands::Integer(v1, v2))

Loading…
Cancel
Save