Skip to content

Commit

Permalink
Merge pull request #296 from Qrlew/id_attributes
Browse files Browse the repository at this point in the history
Adding attributes to data_type::Id
  • Loading branch information
ngrislain authored Nov 27, 2024
2 parents 99042c4 + 95fcd47 commit bd2f1b9
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.9.24] - 2024-11-27
### Added
- Add attributes to data_type::Id

## [0.9.24] - 2024-09-27
### Fixed
- mssql and bigquery translator
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 <[email protected]>"]
name = "qrlew"
version = "0.9.24"
version = "0.9.25"
edition = "2021"
description = "Sarus Qrlew Engine"
documentation = "https://docs.rs/qrlew"
Expand Down
104 changes: 97 additions & 7 deletions src/data_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use itertools::Itertools;
use paste::paste;
use std::{
cmp,
collections::{BTreeSet, HashSet},
collections::{BTreeMap, BTreeSet, HashSet},
convert::Infallible,
error, fmt, hash,
marker::Copy,
Expand Down Expand Up @@ -2167,11 +2167,21 @@ pub struct Id {
reference: Option<Arc<Id>>,
/// If entries are unique
unique: bool,
/// Id attributes stored in a BTreeMap in order to be hashable (required by the Hash trait).
attributes: BTreeMap<String, String>,
}

impl Id {
pub fn new(reference: Option<Arc<Id>>, unique: bool) -> Id {
Id { reference, unique }
pub fn new(
reference: Option<Arc<Id>>,
unique: bool,
attributes: BTreeMap<String, String>,
) -> Id {
Id {
reference,
unique,
attributes,
}
}

pub fn reference(&self) -> Option<&Id> {
Expand All @@ -2181,19 +2191,23 @@ impl Id {
pub fn unique(&self) -> bool {
self.unique
}

pub fn attributes(&self) -> &BTreeMap<String, String> {
&self.attributes
}
}

impl From<(Option<Arc<Id>>, bool)> for Id {
fn from(ref_unique: (Option<Arc<Id>>, bool)) -> Self {
let (reference, unique) = ref_unique;
Id::new(reference, unique)
Id::new(reference, unique, BTreeMap::new())
}
}

impl From<(Option<Id>, bool)> for Id {
fn from(ref_unique: (Option<Id>, bool)) -> Self {
let (reference, unique) = ref_unique;
Id::new(reference.map(Arc::new), unique)
Id::new(reference.map(Arc::new), unique, BTreeMap::new())
}
}

Expand All @@ -2220,30 +2234,47 @@ impl Variant for Id {
true
}

//
fn super_union(&self, other: &Self) -> Result<Self> {
let attributes: BTreeMap<String, String> = self
.attributes
.iter()
.filter(|(key, value)| other.attributes.get(*key).map_or(false, |v| v == *value))
.map(|(key, value)| (key.clone(), value.clone()))
.collect();

Ok(Id::new(
if self.reference == other.reference {
self.reference.clone()
} else {
None
},
false,
attributes,
))
}

// if attributes are equal
fn super_intersection(&self, other: &Self) -> Result<Self> {
let attributes: BTreeMap<String, String> = self
.attributes
.iter()
.filter(|(key, value)| other.attributes.get(*key).map_or(false, |v| v == *value))
.map(|(key, value)| (key.clone(), value.clone()))
.collect();

Ok(Id::new(
if self.reference == other.reference {
self.reference.clone()
} else {
None
},
self.unique && other.unique,
attributes,
))
}

fn maximal_superset(&self) -> Result<Self> {
Ok(Id::new(None, false))
Ok(Id::new(None, false, self.attributes().clone()))
}
}

Expand Down Expand Up @@ -4568,4 +4599,63 @@ mod tests {
])
);
}

#[test]
fn test_id() {
let id = Id::new(
None,
false,
BTreeMap::from([("base".to_string(), "string".to_string())]),
);
let left = DataType::Id(id.clone());
let right = DataType::Id(id);

let union = left.super_union(&right).unwrap();
println!("left ∪ right = {}", union);
assert_eq!(union, left);

let intersection = left.super_intersection(&right).unwrap();
println!("left ∩ left = {}", intersection);
assert_eq!(intersection, left);

let left = DataType::Id(Id::new(
None,
false,
BTreeMap::from([("base".to_string(), "string".to_string())]),
));
let right = DataType::Id(Id::new(
None,
false,
BTreeMap::from([("base".to_string(), "int".to_string())]),
));

let union = left.super_union(&right).unwrap();
println!("left ∪ right = {}", union);
assert_eq!(union, DataType::Id(Id::new(None, false, BTreeMap::new())));

let intersection = left.super_intersection(&right).unwrap();
println!("left ∩ right = {}", intersection);
assert_eq!(
intersection,
DataType::Id(Id::new(None, false, BTreeMap::new()))
);

let left = DataType::Id(Id::new(
None,
false,
BTreeMap::from([("base".to_string(), "string".to_string())]),
));
let right = DataType::Id(Id::new(None, false, BTreeMap::new()));

let union = left.super_union(&right).unwrap();
println!("left ∪ right = {}", union);
assert_eq!(union, DataType::Id(Id::new(None, false, BTreeMap::new())));

let intersection = left.super_intersection(&right).unwrap();
println!("left ∩ right = {}", intersection);
assert_eq!(
intersection,
DataType::Id(Id::new(None, false, BTreeMap::new()))
);
}
}

0 comments on commit bd2f1b9

Please sign in to comment.