Skip to content

Commit

Permalink
Merge pull request #283 from Qrlew/reduce_cost_superimage
Browse files Browse the repository at this point in the history
ok tests and version
  • Loading branch information
ngrislain authored Jul 4, 2024
2 parents 9603c8f + 5d4bc36 commit 1c6197c
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 18 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.9.21] - 2024-06-27
### Changed
- Add integer domains to few boolean functions to reduce timing of fun.super_image
- Break equality for some datatype by changing DataType's is_subset_of during the Variant trait implementation.

## [0.9.20] - 2024-06-20
### Added
- support for extract_epoch function
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
authors = ["Nicolas Grislain <ng@sarus.tech>"]
name = "qrlew"
version = "0.9.20"
version = "0.9.21"
edition = "2021"
description = "Sarus Qrlew Engine"
documentation = "https://docs.rs/qrlew"
Expand Down
54 changes: 52 additions & 2 deletions src/data_type/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,10 @@ pub fn pi() -> impl Function {

pub fn gt() -> impl Function {
Polymorphic::default()
.with(PartitionnedMonotonic::bivariate(
(data_type::Integer::default(), data_type::Integer::default()),
|a, b| (a > b),
))
.with(PartitionnedMonotonic::bivariate(
(data_type::Float::default(), data_type::Float::default()),
|a, b| (a > b),
Expand Down Expand Up @@ -1474,6 +1478,10 @@ pub fn gt() -> impl Function {

pub fn lt() -> impl Function {
Polymorphic::default()
.with(PartitionnedMonotonic::bivariate(
(data_type::Integer::default(), data_type::Integer::default()),
|a, b| (a < b),
))
.with(PartitionnedMonotonic::bivariate(
(data_type::Float::default(), data_type::Float::default()),
|a, b| (a < b),
Expand Down Expand Up @@ -1501,6 +1509,10 @@ pub fn lt() -> impl Function {

pub fn gt_eq() -> impl Function {
Polymorphic::default()
.with(PartitionnedMonotonic::bivariate(
(data_type::Integer::default(), data_type::Integer::default()),
|a, b| (a >= b),
))
.with(PartitionnedMonotonic::bivariate(
(data_type::Float::default(), data_type::Float::default()),
|a, b| (a >= b),
Expand Down Expand Up @@ -1528,6 +1540,10 @@ pub fn gt_eq() -> impl Function {

pub fn lt_eq() -> impl Function {
Polymorphic::default()
.with(PartitionnedMonotonic::bivariate(
(data_type::Integer::default(), data_type::Integer::default()),
|a, b| (a <= b),
))
.with(PartitionnedMonotonic::bivariate(
(data_type::Float::default(), data_type::Float::default()),
|a, b| (a <= b),
Expand Down Expand Up @@ -1946,10 +1962,17 @@ pub fn extract_year() -> impl Function {

pub fn extract_epoch() -> impl Function {
Polymorphic::default()
.with(PartitionnedMonotonic::univariate(
data_type::Date::default(),
|a| {
(a.and_hms_opt(0, 0, 0).unwrap().and_utc().timestamp() as i64)
.clamp(<i64 as Bound>::min(), <i64 as Bound>::max())
},
))
.with(PartitionnedMonotonic::univariate(
data_type::DateTime::default(),
|a| {
(a.and_utc().timestamp() as i64).clamp(<i64 as Bound>::min(), <i64 as Bound>::max())
(a.and_utc().timestamp() as f64).clamp(<f64 as Bound>::min(), <f64 as Bound>::max())
},
))
.with(PartitionnedMonotonic::univariate(
Expand Down Expand Up @@ -2806,6 +2829,24 @@ mod tests {
assert_eq!(val, Value::from(true));
}

#[test]
fn test_gt_large_int_interval() {
println!("Test gt");
let fun = gt();
println!("type = {}", fun);
println!("domain = {}", fun.domain());
println!("co_domain = {}", fun.co_domain());

let set = DataType::from(Struct::from_data_types([
DataType::from(data_type::Integer::from_interval(1, 100000)),
DataType::from(data_type::Integer::from_interval(1, 100000)),
]));

// let set = DataType::integer_interval(1, 100000);
let im = fun.super_image(&set).unwrap();
println!("\nim({}) = {}", set, im);
}

#[test]
fn test_gt() {
println!("Test gt");
Expand Down Expand Up @@ -4351,7 +4392,7 @@ mod tests {

let im = fun.super_image(&set).unwrap();
println!("im({}) = {}", set, im);
assert!(im == DataType::integer_values([1467969011, 1783502111]));
assert!(im == DataType::float_values([1467969011.0, 1783502111.0]));

let set = DataType::duration_values([
chrono::Duration::hours(24),
Expand All @@ -4361,6 +4402,15 @@ mod tests {
println!("im({}) = {}", set, im);
assert!(im == DataType::integer_values([86400, 100]));

let set = DataType::date_values([
chrono::NaiveDate::from_ymd_opt(1980, 12, 06).unwrap(),
chrono::NaiveDate::from_ymd_opt(1981, 4, 20).unwrap(),
]);

let im = fun.super_image(&set).unwrap();
println!("im({}) = {}", set, im);
assert!(im == DataType::integer_values([344908800, 356572800]));

// year
println!("\nTest extract_year");
let fun = extract_year();
Expand Down
83 changes: 72 additions & 11 deletions src/data_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1503,7 +1503,7 @@ impl Index<usize> for Union {
}

/// Optional variant
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct Optional {
data_type: Arc<DataType>,
}
Expand Down Expand Up @@ -1538,13 +1538,6 @@ impl From<DataType> for Optional {
}
}

#[allow(clippy::derive_hash_xor_eq)]
impl hash::Hash for Optional {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.data_type.hash(state);
}
}

impl cmp::PartialOrd for Optional {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
partial_cmp(self, other)
Expand Down Expand Up @@ -2558,10 +2551,78 @@ impl Variant for DataType {
(DataType::Bytes(_), DataType::Bytes(_)) => true,
(_, DataType::Any) => true,
(DataType::Any, _) => false,
(s, o) => s
(s, DataType::Text(o)) => {
let o = DataType::Text(o.clone());
s.clone()
.into_data_type(&o)
.map_or(false, |s| s.is_subset_of(&o))
}
(s, DataType::Null) => s
.clone()
.into_data_type(o)
.map_or(false, |s| s.is_subset_of(o)),
.into_data_type(&DataType::Null)
.map_or(false, |s| s.is_subset_of(&DataType::Null)),
(DataType::Float(s), DataType::Integer(o)) => {
let left = DataType::Float(s.clone());
let right = DataType::Integer(o.clone());
left.into_data_type(&right)
.map_or(false, |left| left.is_subset_of(&right))
}
(DataType::Integer(s), DataType::Float(o)) => {
let left = DataType::Integer(s.clone());
let right = DataType::Float(o.clone());
left.into_data_type(&right)
.map_or(false, |left| left.is_subset_of(&right))
}
(DataType::Boolean(s), DataType::Float(o)) => {
let left = DataType::Boolean(s.clone());
let right = DataType::Float(o.clone());
left.into_data_type(&right)
.map_or(false, |left| left.is_subset_of(&right))
}
(DataType::Float(s), DataType::Boolean(o)) => {
let left = DataType::Float(s.clone());
let right = DataType::Boolean(o.clone());
left.into_data_type(&right)
.map_or(false, |left| left.is_subset_of(&right))
}
(DataType::Boolean(s), DataType::Integer(o)) => {
let left = DataType::Boolean(s.clone());
let right = DataType::Integer(o.clone());
left.into_data_type(&right)
.map_or(false, |left| left.is_subset_of(&right))
}
(DataType::Integer(s), DataType::Boolean(o)) => {
let left = DataType::Integer(s.clone());
let right = DataType::Boolean(o.clone());
left.into_data_type(&right)
.map_or(false, |left| left.is_subset_of(&right))
}
(DataType::Date(s), DataType::DateTime(o)) => {
let left = DataType::Date(s.clone());
let right = DataType::DateTime(o.clone());
left.into_data_type(&right)
.map_or(false, |left| left.is_subset_of(&right))
}
(DataType::DateTime(s), DataType::Date(o)) => {
let left = DataType::DateTime(s.clone());
let right = DataType::Date(o.clone());
left.into_data_type(&right)
.map_or(false, |left| left.is_subset_of(&right))
}
(s, DataType::Union(o)) => {
let right = DataType::Union(o.clone());
s.clone()
.into_data_type(&right)
.map_or(false, |left| left.is_subset_of(&right))
}
(s, DataType::Optional(o)) => {
let right = DataType::Optional(o.clone());
s.clone()
.into_data_type(&right)
.map_or(false, |left| left.is_subset_of(&right))
}
// let's try to be conservative. For any other combination return false
_ => false,
}
}
)
Expand Down
8 changes: 4 additions & 4 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ pub trait Acceptor<'a>: 'a + Sized + Debug + Eq + Hash {
for (_a, s) in Iterator::new(visitor, self) {
state = s
}
if let State::Accept(output) = state {
output.clone()
} else {
panic!()
match state {
State::Push => panic!("Found a `Push` state for Acceptor: {:?}. This should not be possible at this point.", self),
State::Visit => panic!("Found a `Visit` state for Acceptor: {:?}. This should not be possible at this point.", self),
State::Accept(output) => output.clone(),
}
}

Expand Down

0 comments on commit 1c6197c

Please sign in to comment.