Drops EncodedTerm Ord and PartialOrd implementations

pull/22/head
Tpt 5 years ago committed by Tpt
parent 8c115abc19
commit 8aa5bf4ceb
  1. 1
      lib/Cargo.toml
  2. 88
      lib/src/sparql/eval.rs
  3. 140
      lib/src/store/numeric_encoder.rs

@ -17,7 +17,6 @@ lazy_static = "1"
rocksdb = { version = "0.13", optional = true }
byteorder = { version = "1", features = ["i128"] }
quick-xml = "0.17"
ordered-float = "1"
num-traits = "0.2"
chrono = "0.4"
rand = "0.7"

@ -840,15 +840,15 @@ impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> {
NumericBinaryOperands::Decimal(v1, v2) => v1.checked_div(v2)?.into(),
}),
PlanExpression::UnaryPlus(e) => match self.eval_expression(e, tuple)? {
EncodedTerm::FloatLiteral(value) => Some((*value).into()),
EncodedTerm::DoubleLiteral(value) => Some((*value).into()),
EncodedTerm::IntegerLiteral(value) => Some((value).into()),
EncodedTerm::DecimalLiteral(value) => Some((value).into()),
EncodedTerm::FloatLiteral(value) => Some(value.into()),
EncodedTerm::DoubleLiteral(value) => Some(value.into()),
EncodedTerm::IntegerLiteral(value) => Some(value.into()),
EncodedTerm::DecimalLiteral(value) => Some(value.into()),
_ => None,
},
PlanExpression::UnaryMinus(e) => match self.eval_expression(e, tuple)? {
EncodedTerm::FloatLiteral(value) => Some((-*value).into()),
EncodedTerm::DoubleLiteral(value) => Some((-*value).into()),
EncodedTerm::FloatLiteral(value) => Some((-value).into()),
EncodedTerm::DoubleLiteral(value) => Some((-value).into()),
EncodedTerm::IntegerLiteral(value) => Some((-value).into()),
EncodedTerm::DecimalLiteral(value) => Some((-value).into()),
_ => None,
@ -1362,8 +1362,8 @@ impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> {
_ => None,
},
PlanExpression::DecimalCast(e) => match self.eval_expression(e, tuple)? {
EncodedTerm::FloatLiteral(value) => Some(Decimal::from_f32(*value).into()),
EncodedTerm::DoubleLiteral(value) => Some(Decimal::from_f64(*value).into()),
EncodedTerm::FloatLiteral(value) => Some(Decimal::from_f32(value).into()),
EncodedTerm::DoubleLiteral(value) => Some(Decimal::from_f64(value).into()),
EncodedTerm::IntegerLiteral(value) => Some(Decimal::from(value).into()),
EncodedTerm::DecimalLiteral(value) => Some(value.into()),
EncodedTerm::BooleanLiteral(value) => {
@ -1411,8 +1411,8 @@ impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> {
match term {
EncodedTerm::BooleanLiteral(value) => Some(value),
EncodedTerm::StringLiteral { .. } => Some(term != ENCODED_EMPTY_STRING_LITERAL),
EncodedTerm::FloatLiteral(value) => Some(*value != 0f32),
EncodedTerm::DoubleLiteral(value) => Some(*value != 0f64),
EncodedTerm::FloatLiteral(value) => Some(value != 0f32),
EncodedTerm::DoubleLiteral(value) => Some(value != 0f64),
EncodedTerm::IntegerLiteral(value) => Some(value != 0),
EncodedTerm::DecimalLiteral(value) => Some(value != Decimal::from(0)),
_ => None,
@ -1651,31 +1651,31 @@ impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> {
},
EncodedTerm::FloatLiteral(a) => match b {
EncodedTerm::FloatLiteral(b) => Some(a == b),
EncodedTerm::DoubleLiteral(b) => Some(a.to_f64()? == *b),
EncodedTerm::IntegerLiteral(b) => Some(*a == b.to_f32()?),
EncodedTerm::DecimalLiteral(b) => Some(*a == b.to_f32()),
EncodedTerm::DoubleLiteral(b) => Some(a.to_f64()? == b),
EncodedTerm::IntegerLiteral(b) => Some(a == b.to_f32()?),
EncodedTerm::DecimalLiteral(b) => Some(a == b.to_f32()),
EncodedTerm::TypedLiteral { .. } => None,
_ => Some(false),
},
EncodedTerm::DoubleLiteral(a) => match b {
EncodedTerm::FloatLiteral(b) => Some(*a == b.to_f64()?),
EncodedTerm::FloatLiteral(b) => Some(a == b.to_f64()?),
EncodedTerm::DoubleLiteral(b) => Some(a == b),
EncodedTerm::IntegerLiteral(b) => Some(*a == b.to_f64()?),
EncodedTerm::DecimalLiteral(b) => Some(*a == b.to_f64()),
EncodedTerm::IntegerLiteral(b) => Some(a == b.to_f64()?),
EncodedTerm::DecimalLiteral(b) => Some(a == b.to_f64()),
EncodedTerm::TypedLiteral { .. } => None,
_ => Some(false),
},
EncodedTerm::IntegerLiteral(a) => match b {
EncodedTerm::FloatLiteral(b) => Some(a.to_f32()? == *b),
EncodedTerm::DoubleLiteral(b) => Some(a.to_f64()? == *b),
EncodedTerm::FloatLiteral(b) => Some(a.to_f32()? == b),
EncodedTerm::DoubleLiteral(b) => Some(a.to_f64()? == b),
EncodedTerm::IntegerLiteral(b) => Some(a == b),
EncodedTerm::DecimalLiteral(b) => Some(Decimal::from(a) == b),
EncodedTerm::TypedLiteral { .. } => None,
_ => Some(false),
},
EncodedTerm::DecimalLiteral(a) => match b {
EncodedTerm::FloatLiteral(b) => Some(a.to_f32() == *b),
EncodedTerm::DoubleLiteral(b) => Some(a.to_f64() == *b),
EncodedTerm::FloatLiteral(b) => Some(a.to_f32() == b),
EncodedTerm::DoubleLiteral(b) => Some(a.to_f64() == b),
EncodedTerm::IntegerLiteral(b) => Some(a == Decimal::from(b)),
EncodedTerm::DecimalLiteral(b) => Some(a == b),
EncodedTerm::TypedLiteral { .. } => None,
@ -1796,29 +1796,29 @@ impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> {
}
}
EncodedTerm::FloatLiteral(a) => match b {
EncodedTerm::FloatLiteral(b) => (*a).partial_cmp(&*b),
EncodedTerm::DoubleLiteral(b) => a.to_f64()?.partial_cmp(&*b),
EncodedTerm::IntegerLiteral(b) => (*a).partial_cmp(&b.to_f32()?),
EncodedTerm::DecimalLiteral(b) => (*a).partial_cmp(&b.to_f32()),
EncodedTerm::FloatLiteral(b) => a.partial_cmp(&b),
EncodedTerm::DoubleLiteral(b) => a.to_f64()?.partial_cmp(&b),
EncodedTerm::IntegerLiteral(b) => a.partial_cmp(&b.to_f32()?),
EncodedTerm::DecimalLiteral(b) => a.partial_cmp(&b.to_f32()),
_ => None,
},
EncodedTerm::DoubleLiteral(a) => match b {
EncodedTerm::FloatLiteral(b) => (*a).partial_cmp(&b.to_f64()?),
EncodedTerm::DoubleLiteral(b) => (*a).partial_cmp(&*b),
EncodedTerm::IntegerLiteral(b) => (*a).partial_cmp(&b.to_f64()?),
EncodedTerm::DecimalLiteral(b) => (*a).partial_cmp(&b.to_f64()),
EncodedTerm::FloatLiteral(b) => a.partial_cmp(&b.to_f64()?),
EncodedTerm::DoubleLiteral(b) => a.partial_cmp(&b),
EncodedTerm::IntegerLiteral(b) => a.partial_cmp(&b.to_f64()?),
EncodedTerm::DecimalLiteral(b) => a.partial_cmp(&b.to_f64()),
_ => None,
},
EncodedTerm::IntegerLiteral(a) => match b {
EncodedTerm::FloatLiteral(b) => a.to_f32()?.partial_cmp(&*b),
EncodedTerm::DoubleLiteral(b) => a.to_f64()?.partial_cmp(&*b),
EncodedTerm::FloatLiteral(b) => a.to_f32()?.partial_cmp(&b),
EncodedTerm::DoubleLiteral(b) => a.to_f64()?.partial_cmp(&b),
EncodedTerm::IntegerLiteral(b) => a.partial_cmp(&b),
EncodedTerm::DecimalLiteral(b) => Decimal::from(a).partial_cmp(&b),
_ => None,
},
EncodedTerm::DecimalLiteral(a) => match b {
EncodedTerm::FloatLiteral(b) => a.to_f32().partial_cmp(&*b),
EncodedTerm::DoubleLiteral(b) => a.to_f64().partial_cmp(&*b),
EncodedTerm::FloatLiteral(b) => a.to_f32().partial_cmp(&b),
EncodedTerm::DoubleLiteral(b) => a.to_f64().partial_cmp(&b),
EncodedTerm::IntegerLiteral(b) => a.partial_cmp(&Decimal::from(b)),
EncodedTerm::DecimalLiteral(b) => a.partial_cmp(&b),
_ => None,
@ -1885,34 +1885,34 @@ impl NumericBinaryOperands {
fn new(a: EncodedTerm, b: EncodedTerm) -> Option<Self> {
match (a, b) {
(EncodedTerm::FloatLiteral(v1), EncodedTerm::FloatLiteral(v2)) => {
Some(NumericBinaryOperands::Float(*v1, v2.to_f32()?))
Some(NumericBinaryOperands::Float(v1, v2.to_f32()?))
}
(EncodedTerm::FloatLiteral(v1), EncodedTerm::DoubleLiteral(v2)) => {
Some(NumericBinaryOperands::Double(v1.to_f64()?, *v2))
Some(NumericBinaryOperands::Double(v1.to_f64()?, v2))
}
(EncodedTerm::FloatLiteral(v1), EncodedTerm::IntegerLiteral(v2)) => {
Some(NumericBinaryOperands::Float(*v1, v2.to_f32()?))
Some(NumericBinaryOperands::Float(v1, v2.to_f32()?))
}
(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)) => {
Some(NumericBinaryOperands::Double(*v1, v2.to_f64()?))
Some(NumericBinaryOperands::Double(v1, v2.to_f64()?))
}
(EncodedTerm::DoubleLiteral(v1), EncodedTerm::DoubleLiteral(v2)) => {
Some(NumericBinaryOperands::Double(*v1, *v2))
Some(NumericBinaryOperands::Double(v1, v2))
}
(EncodedTerm::DoubleLiteral(v1), EncodedTerm::IntegerLiteral(v2)) => {
Some(NumericBinaryOperands::Double(*v1, v2.to_f64()?))
Some(NumericBinaryOperands::Double(v1, v2.to_f64()?))
}
(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)) => {
Some(NumericBinaryOperands::Float(v1.to_f32()?, *v2))
Some(NumericBinaryOperands::Float(v1.to_f32()?, v2))
}
(EncodedTerm::IntegerLiteral(v1), EncodedTerm::DoubleLiteral(v2)) => {
Some(NumericBinaryOperands::Double(v1.to_f64()?, *v2))
Some(NumericBinaryOperands::Double(v1.to_f64()?, v2))
}
(EncodedTerm::IntegerLiteral(v1), EncodedTerm::IntegerLiteral(v2)) => {
Some(NumericBinaryOperands::Integer(v1, v2))
@ -1921,10 +1921,10 @@ impl NumericBinaryOperands {
Some(NumericBinaryOperands::Decimal(Decimal::from(v1), v2))
}
(EncodedTerm::DecimalLiteral(v1), EncodedTerm::FloatLiteral(v2)) => {
Some(NumericBinaryOperands::Float(v1.to_f32(), *v2))
Some(NumericBinaryOperands::Float(v1.to_f32(), v2))
}
(EncodedTerm::DecimalLiteral(v1), EncodedTerm::DoubleLiteral(v2)) => {
Some(NumericBinaryOperands::Double(v1.to_f64(), *v2))
Some(NumericBinaryOperands::Double(v1.to_f64(), v2))
}
(EncodedTerm::DecimalLiteral(v1), EncodedTerm::IntegerLiteral(v2)) => {
Some(NumericBinaryOperands::Decimal(v1, Decimal::from(v2)))

@ -9,10 +9,11 @@ use chrono::prelude::*;
use failure::format_err;
use md5::digest::Digest;
use md5::Md5;
use ordered_float::OrderedFloat;
use rand::random;
use rio_api::model as rio;
use std::collections::HashMap;
use std::hash::Hash;
use std::hash::Hasher;
use std::io::Read;
use std::io::Write;
use std::mem::size_of;
@ -89,7 +90,7 @@ pub const ENCODED_XSD_DATE_TIME_NAMED_NODE: EncodedTerm = EncodedTerm::NamedNode
iri_id: XSD_DATE_TIME_ID,
};
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy, Hash)]
#[derive(Debug, Clone, Copy)]
pub enum EncodedTerm {
DefaultGraph,
NamedNode { iri_id: u128 },
@ -98,8 +99,8 @@ pub enum EncodedTerm {
LangStringLiteral { value_id: u128, language_id: u128 },
TypedLiteral { value_id: u128, datatype_id: u128 },
BooleanLiteral(bool),
FloatLiteral(OrderedFloat<f32>),
DoubleLiteral(OrderedFloat<f64>),
FloatLiteral(f32),
DoubleLiteral(f64),
IntegerLiteral(i64),
DecimalLiteral(Decimal),
DateLiteral(Date<FixedOffset>),
@ -109,6 +110,109 @@ pub enum EncodedTerm {
NaiveDateTimeLiteral(NaiveDateTime),
}
impl PartialEq for EncodedTerm {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(EncodedTerm::DefaultGraph, EncodedTerm::DefaultGraph) => true,
(
EncodedTerm::NamedNode { iri_id: iri_id_a },
EncodedTerm::NamedNode { iri_id: iri_id_b },
) => iri_id_a == iri_id_b,
(EncodedTerm::BlankNode { id: id_a }, EncodedTerm::BlankNode { id: id_b }) => {
id_a == id_b
}
(
EncodedTerm::StringLiteral {
value_id: value_id_a,
},
EncodedTerm::StringLiteral {
value_id: value_id_b,
},
) => value_id_a == value_id_b,
(
EncodedTerm::LangStringLiteral {
value_id: value_id_a,
language_id: language_id_a,
},
EncodedTerm::LangStringLiteral {
value_id: value_id_b,
language_id: language_id_b,
},
) => value_id_a == value_id_b && language_id_a == language_id_b,
(
EncodedTerm::TypedLiteral {
value_id: value_id_a,
datatype_id: datatype_id_a,
},
EncodedTerm::TypedLiteral {
value_id: value_id_b,
datatype_id: datatype_id_b,
},
) => value_id_a == value_id_b && datatype_id_a == datatype_id_b,
(EncodedTerm::BooleanLiteral(a), EncodedTerm::BooleanLiteral(b)) => a == b,
(EncodedTerm::FloatLiteral(a), EncodedTerm::FloatLiteral(b)) => {
if a.is_nan() {
b.is_nan()
} else {
a == b
}
}
(EncodedTerm::DoubleLiteral(a), EncodedTerm::DoubleLiteral(b)) => {
if a.is_nan() {
b.is_nan()
} else {
a == b
}
}
(EncodedTerm::IntegerLiteral(a), EncodedTerm::IntegerLiteral(b)) => a == b,
(EncodedTerm::DecimalLiteral(a), EncodedTerm::DecimalLiteral(b)) => a == b,
(EncodedTerm::DateLiteral(a), EncodedTerm::DateLiteral(b)) => a == b,
(EncodedTerm::NaiveDateLiteral(a), EncodedTerm::NaiveDateLiteral(b)) => a == b,
(EncodedTerm::NaiveTimeLiteral(a), EncodedTerm::NaiveTimeLiteral(b)) => a == b,
(EncodedTerm::DateTimeLiteral(a), EncodedTerm::DateTimeLiteral(b)) => a == b,
(EncodedTerm::NaiveDateTimeLiteral(a), EncodedTerm::NaiveDateTimeLiteral(b)) => a == b,
(_, _) => false,
}
}
}
impl Eq for EncodedTerm {}
impl Hash for EncodedTerm {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
EncodedTerm::NamedNode { iri_id } => iri_id.hash(state),
EncodedTerm::BlankNode { id } => id.hash(state),
EncodedTerm::DefaultGraph => (),
EncodedTerm::StringLiteral { value_id } => value_id.hash(state),
EncodedTerm::LangStringLiteral {
value_id,
language_id,
} => {
value_id.hash(state);
language_id.hash(state);
}
EncodedTerm::TypedLiteral {
value_id,
datatype_id,
} => {
value_id.hash(state);
datatype_id.hash(state);
}
EncodedTerm::BooleanLiteral(value) => value.hash(state),
EncodedTerm::FloatLiteral(value) => state.write(&value.to_ne_bytes()),
EncodedTerm::DoubleLiteral(value) => state.write(&value.to_ne_bytes()),
EncodedTerm::IntegerLiteral(value) => value.hash(state),
EncodedTerm::DecimalLiteral(value) => value.hash(state),
EncodedTerm::DateLiteral(value) => value.hash(state),
EncodedTerm::NaiveDateLiteral(value) => value.hash(state),
EncodedTerm::NaiveTimeLiteral(value) => value.hash(state),
EncodedTerm::DateTimeLiteral(value) => value.hash(state),
EncodedTerm::NaiveDateTimeLiteral(value) => value.hash(state),
}
}
}
impl EncodedTerm {
pub fn is_named_node(&self) -> bool {
match self {
@ -214,13 +318,13 @@ impl From<u32> for EncodedTerm {
impl From<f32> for EncodedTerm {
fn from(value: f32) -> Self {
EncodedTerm::FloatLiteral(value.into())
EncodedTerm::FloatLiteral(value)
}
}
impl From<f64> for EncodedTerm {
fn from(value: f64) -> Self {
EncodedTerm::DoubleLiteral(value.into())
EncodedTerm::DoubleLiteral(value)
}
}
@ -364,7 +468,7 @@ impl From<&Term> for EncodedTerm {
}
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct EncodedQuad {
pub subject: EncodedTerm,
pub predicate: EncodedTerm,
@ -435,12 +539,8 @@ impl<R: Read> TermReader for R {
}),
TYPE_BOOLEAN_LITERAL_TRUE => Ok(EncodedTerm::BooleanLiteral(true)),
TYPE_BOOLEAN_LITERAL_FALSE => Ok(EncodedTerm::BooleanLiteral(false)),
TYPE_FLOAT_LITERAL => Ok(EncodedTerm::FloatLiteral(OrderedFloat(
self.read_f32::<LittleEndian>()?,
))),
TYPE_DOUBLE_LITERAL => Ok(EncodedTerm::DoubleLiteral(OrderedFloat(
self.read_f64::<LittleEndian>()?,
))),
TYPE_FLOAT_LITERAL => Ok(EncodedTerm::FloatLiteral(self.read_f32::<LittleEndian>()?)),
TYPE_DOUBLE_LITERAL => Ok(EncodedTerm::DoubleLiteral(self.read_f64::<LittleEndian>()?)),
TYPE_INTEGER_LITERAL => Ok(EncodedTerm::IntegerLiteral(
self.read_i64::<LittleEndian>()?,
)),
@ -956,17 +1056,11 @@ pub fn parse_boolean_str(value: &str) -> Option<EncodedTerm> {
}
pub fn parse_float_str(value: &str) -> Option<EncodedTerm> {
value
.parse()
.map(|value| EncodedTerm::FloatLiteral(OrderedFloat(value)))
.ok()
value.parse().map(EncodedTerm::FloatLiteral).ok()
}
pub fn parse_double_str(value: &str) -> Option<EncodedTerm> {
value
.parse()
.map(|value| EncodedTerm::DoubleLiteral(OrderedFloat(value)))
.ok()
value.parse().map(EncodedTerm::DoubleLiteral).ok()
}
pub fn parse_integer_str(value: &str) -> Option<EncodedTerm> {
@ -1089,8 +1183,8 @@ impl<S: StrLookup> Decoder for S {
)
.into()),
EncodedTerm::BooleanLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::FloatLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::DoubleLiteral(value) => Ok(Literal::from(*value).into()),
EncodedTerm::FloatLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::DoubleLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::IntegerLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::DecimalLiteral(value) => Ok(Literal::from(value).into()),
EncodedTerm::DateLiteral(value) => Ok(Literal::from(value).into()),

Loading…
Cancel
Save