Simplifies some code in the SPARQL evaluator

pull/46/head
Tpt 4 years ago
parent 65b763b09a
commit b90fbdc7fe
  1. 58
      lib/src/sparql/eval.rs
  2. 7
      lib/src/store/numeric_encoder.rs

@ -1057,13 +1057,7 @@ where
if e.is_named_node() { if e.is_named_node() {
Some(e) Some(e)
} else { } else {
let iri = match e { let iri = self.to_simple_string(e)?;
EncodedTerm::SmallStringLiteral(value) => Some(value.into()),
EncodedTerm::BigStringLiteral { value_id } => {
self.dataset.get_str(value_id).ok()?
}
_ => None,
}?;
self.build_named_node( self.build_named_node(
&if let Some(base_iri) = &self.base_iri { &if let Some(base_iri) = &self.base_iri {
base_iri.resolve(&iri) base_iri.resolve(&iri)
@ -1470,7 +1464,6 @@ where
EncodedTerm::BooleanLiteral(value) => { EncodedTerm::BooleanLiteral(value) => {
Some(if value { 1_f32 } else { 0_f32 }.into()) Some(if value { 1_f32 } else { 0_f32 }.into())
} }
EncodedTerm::SmallStringLiteral(value) => parse_float_str(&value), EncodedTerm::SmallStringLiteral(value) => parse_float_str(&value),
EncodedTerm::BigStringLiteral { value_id } => { EncodedTerm::BigStringLiteral { value_id } => {
parse_float_str(&*self.dataset.get_str(value_id).ok()??) parse_float_str(&*self.dataset.get_str(value_id).ok()??)
@ -1916,7 +1909,7 @@ where
}, },
EncodedTerm::BooleanLiteral(a) => match b { EncodedTerm::BooleanLiteral(a) => match b {
EncodedTerm::BooleanLiteral(b) => Some(a == b), EncodedTerm::BooleanLiteral(b) => Some(a == b),
EncodedTerm::SmallTypedLiteral { .. } | EncodedTerm::BigTypedLiteral { .. } => None, _ if b.is_unknown_typed_literal() => None,
_ => Some(false), _ => Some(false),
}, },
EncodedTerm::FloatLiteral(a) => match b { EncodedTerm::FloatLiteral(a) => match b {
@ -1924,7 +1917,7 @@ where
EncodedTerm::DoubleLiteral(b) => Some(f64::from(a) == b), EncodedTerm::DoubleLiteral(b) => Some(f64::from(a) == b),
EncodedTerm::IntegerLiteral(b) => Some(a == b as 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::SmallTypedLiteral { .. } | EncodedTerm::BigTypedLiteral { .. } => None, _ if b.is_unknown_typed_literal() => None,
_ => Some(false), _ => Some(false),
}, },
EncodedTerm::DoubleLiteral(a) => match b { EncodedTerm::DoubleLiteral(a) => match b {
@ -1932,7 +1925,7 @@ where
EncodedTerm::DoubleLiteral(b) => Some(a == b), EncodedTerm::DoubleLiteral(b) => Some(a == b),
EncodedTerm::IntegerLiteral(b) => Some(a == (b as 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::SmallTypedLiteral { .. } | EncodedTerm::BigTypedLiteral { .. } => None, _ if b.is_unknown_typed_literal() => None,
_ => Some(false), _ => Some(false),
}, },
EncodedTerm::IntegerLiteral(a) => match b { EncodedTerm::IntegerLiteral(a) => match b {
@ -1940,7 +1933,7 @@ where
EncodedTerm::DoubleLiteral(b) => Some((a as 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::SmallTypedLiteral { .. } | EncodedTerm::BigTypedLiteral { .. } => None, _ if b.is_unknown_typed_literal() => None,
_ => Some(false), _ => Some(false),
}, },
EncodedTerm::DecimalLiteral(a) => match b { EncodedTerm::DecimalLiteral(a) => match b {
@ -1948,68 +1941,68 @@ where
EncodedTerm::DoubleLiteral(b) => Some(a.to_f64() == b), EncodedTerm::DoubleLiteral(b) => Some(a.to_f64() == b),
EncodedTerm::IntegerLiteral(b) => Some(a == Decimal::from(b)), EncodedTerm::IntegerLiteral(b) => Some(a == Decimal::from(b)),
EncodedTerm::DecimalLiteral(b) => Some(a == b), EncodedTerm::DecimalLiteral(b) => Some(a == b),
EncodedTerm::SmallTypedLiteral { .. } | EncodedTerm::BigTypedLiteral { .. } => None, _ if b.is_unknown_typed_literal() => None,
_ => Some(false), _ => Some(false),
}, },
EncodedTerm::DateTimeLiteral(a) => match b { EncodedTerm::DateTimeLiteral(a) => match b {
EncodedTerm::DateTimeLiteral(b) => Some(a == b), EncodedTerm::DateTimeLiteral(b) => Some(a == b),
EncodedTerm::SmallTypedLiteral { .. } | EncodedTerm::BigTypedLiteral { .. } => None, _ if b.is_unknown_typed_literal() => None,
_ => Some(false), _ => Some(false),
}, },
EncodedTerm::TimeLiteral(a) => match b { EncodedTerm::TimeLiteral(a) => match b {
EncodedTerm::TimeLiteral(b) => Some(a == b), EncodedTerm::TimeLiteral(b) => Some(a == b),
EncodedTerm::SmallTypedLiteral { .. } | EncodedTerm::BigTypedLiteral { .. } => None, _ if b.is_unknown_typed_literal() => None,
_ => Some(false), _ => Some(false),
}, },
EncodedTerm::DateLiteral(a) => match b { EncodedTerm::DateLiteral(a) => match b {
EncodedTerm::DateLiteral(b) => Some(a == b), EncodedTerm::DateLiteral(b) => Some(a == b),
EncodedTerm::SmallTypedLiteral { .. } | EncodedTerm::BigTypedLiteral { .. } => None, _ if b.is_unknown_typed_literal() => None,
_ => Some(false), _ => Some(false),
}, },
EncodedTerm::GYearMonthLiteral(a) => match b { EncodedTerm::GYearMonthLiteral(a) => match b {
EncodedTerm::GYearMonthLiteral(b) => Some(a == b), EncodedTerm::GYearMonthLiteral(b) => Some(a == b),
EncodedTerm::SmallTypedLiteral { .. } | EncodedTerm::BigTypedLiteral { .. } => None, _ if b.is_unknown_typed_literal() => None,
_ => Some(false), _ => Some(false),
}, },
EncodedTerm::GYearLiteral(a) => match b { EncodedTerm::GYearLiteral(a) => match b {
EncodedTerm::GYearLiteral(b) => Some(a == b), EncodedTerm::GYearLiteral(b) => Some(a == b),
EncodedTerm::SmallTypedLiteral { .. } | EncodedTerm::BigTypedLiteral { .. } => None, _ if b.is_unknown_typed_literal() => None,
_ => Some(false), _ => Some(false),
}, },
EncodedTerm::GMonthDayLiteral(a) => match b { EncodedTerm::GMonthDayLiteral(a) => match b {
EncodedTerm::GMonthDayLiteral(b) => Some(a == b), EncodedTerm::GMonthDayLiteral(b) => Some(a == b),
EncodedTerm::SmallTypedLiteral { .. } | EncodedTerm::BigTypedLiteral { .. } => None, _ if b.is_unknown_typed_literal() => None,
_ => Some(false), _ => Some(false),
}, },
EncodedTerm::GDayLiteral(a) => match b { EncodedTerm::GDayLiteral(a) => match b {
EncodedTerm::GDayLiteral(b) => Some(a == b), EncodedTerm::GDayLiteral(b) => Some(a == b),
EncodedTerm::SmallTypedLiteral { .. } | EncodedTerm::BigTypedLiteral { .. } => None, _ if b.is_unknown_typed_literal() => None,
_ => Some(false), _ => Some(false),
}, },
EncodedTerm::GMonthLiteral(a) => match b { EncodedTerm::GMonthLiteral(a) => match b {
EncodedTerm::GMonthLiteral(b) => Some(a == b), EncodedTerm::GMonthLiteral(b) => Some(a == b),
EncodedTerm::SmallTypedLiteral { .. } | EncodedTerm::BigTypedLiteral { .. } => None, _ if b.is_unknown_typed_literal() => None,
_ => Some(false), _ => Some(false),
}, },
EncodedTerm::DurationLiteral(a) => match b { EncodedTerm::DurationLiteral(a) => match b {
EncodedTerm::DurationLiteral(b) => Some(a == b), EncodedTerm::DurationLiteral(b) => Some(a == b),
EncodedTerm::YearMonthDurationLiteral(b) => Some(a == b), EncodedTerm::YearMonthDurationLiteral(b) => Some(a == b),
EncodedTerm::DayTimeDurationLiteral(b) => Some(a == b), EncodedTerm::DayTimeDurationLiteral(b) => Some(a == b),
EncodedTerm::SmallTypedLiteral { .. } | EncodedTerm::BigTypedLiteral { .. } => None, _ if b.is_unknown_typed_literal() => None,
_ => Some(false), _ => Some(false),
}, },
EncodedTerm::YearMonthDurationLiteral(a) => match b { EncodedTerm::YearMonthDurationLiteral(a) => match b {
EncodedTerm::DurationLiteral(b) => Some(a == b), EncodedTerm::DurationLiteral(b) => Some(a == b),
EncodedTerm::YearMonthDurationLiteral(b) => Some(a == b), EncodedTerm::YearMonthDurationLiteral(b) => Some(a == b),
EncodedTerm::DayTimeDurationLiteral(b) => Some(a == b), EncodedTerm::DayTimeDurationLiteral(b) => Some(a == b),
EncodedTerm::SmallTypedLiteral { .. } | EncodedTerm::BigTypedLiteral { .. } => None, _ if b.is_unknown_typed_literal() => None,
_ => Some(false), _ => Some(false),
}, },
EncodedTerm::DayTimeDurationLiteral(a) => match b { EncodedTerm::DayTimeDurationLiteral(a) => match b {
EncodedTerm::DurationLiteral(b) => Some(a == b), EncodedTerm::DurationLiteral(b) => Some(a == b),
EncodedTerm::YearMonthDurationLiteral(b) => Some(a == b), EncodedTerm::YearMonthDurationLiteral(b) => Some(a == b),
EncodedTerm::DayTimeDurationLiteral(b) => Some(a == b), EncodedTerm::DayTimeDurationLiteral(b) => Some(a == b),
EncodedTerm::SmallTypedLiteral { .. } | EncodedTerm::BigTypedLiteral { .. } => None, _ if b.is_unknown_typed_literal() => None,
_ => Some(false), _ => Some(false),
}, },
} }
@ -2034,28 +2027,19 @@ where
) -> Ordering { ) -> Ordering {
match (a, b) { match (a, b) {
(Some(a), Some(b)) => match a { (Some(a), Some(b)) => match a {
EncodedTerm::NumericalBlankNode { .. } _ if a.is_blank_node() => match b {
| EncodedTerm::SmallBlankNode { .. } _ if b.is_blank_node() => Ordering::Equal,
| EncodedTerm::BigBlankNode { .. } => match b {
EncodedTerm::NumericalBlankNode { .. }
| EncodedTerm::SmallBlankNode { .. }
| EncodedTerm::BigBlankNode { .. } => Ordering::Equal,
_ => Ordering::Less, _ => Ordering::Less,
}, },
EncodedTerm::NamedNode { iri_id: a } => match b { EncodedTerm::NamedNode { iri_id: a } => match b {
EncodedTerm::NamedNode { iri_id: b } => { EncodedTerm::NamedNode { iri_id: b } => {
self.compare_str_ids(a, b).unwrap_or(Ordering::Equal) self.compare_str_ids(a, b).unwrap_or(Ordering::Equal)
} }
EncodedTerm::NumericalBlankNode { .. } _ if b.is_blank_node() => Ordering::Greater,
| EncodedTerm::SmallBlankNode { .. }
| EncodedTerm::BigBlankNode { .. } => Ordering::Greater,
_ => Ordering::Less, _ => Ordering::Less,
}, },
a => match b { a => match b {
EncodedTerm::NamedNode { .. } _ if b.is_named_node() || b.is_blank_node() => Ordering::Greater,
| EncodedTerm::NumericalBlankNode { .. }
| EncodedTerm::SmallBlankNode { .. }
| EncodedTerm::BigBlankNode { .. } => Ordering::Greater,
b => self.partial_cmp_literals(a, b).unwrap_or(Ordering::Equal), b => self.partial_cmp_literals(a, b).unwrap_or(Ordering::Equal),
}, },
}, },

@ -301,6 +301,13 @@ impl<I: StrId> EncodedTerm<I> {
} }
} }
pub fn is_unknown_typed_literal(&self) -> bool {
match self {
Self::SmallTypedLiteral { .. } | Self::BigTypedLiteral { .. } => true,
_ => false,
}
}
pub fn is_default_graph(&self) -> bool { pub fn is_default_graph(&self) -> bool {
*self == EncodedTerm::DefaultGraph *self == EncodedTerm::DefaultGraph
} }

Loading…
Cancel
Save