Skip to content

Commit

Permalink
Add metas to PartiQL Commons
Browse files Browse the repository at this point in the history
Adds an implementation for storing metadata for PartiQL objects. Currently, the implementation does not include any traits. It introduces `PartiqlMetadata` and `PartiqlMetaValue` structures:

```rust
        let foo_val = PartiqlMetaValue::String("foo".to_string());
        let i64_val = PartiqlMetaValue::Int64(2);

        let expected_vec_val = vec![foo_val, i64_val];
        let expected_bool_val = true;
        let expected_int_val = 2;
        let expected_float_val = 2.5;
        let expected_str_val = "foo";

        let mut expected_map = PartiqlMetadata::new();
        expected_map.insert("bool value", expected_bool_val.into());
        expected_map.insert("integer value", expected_int_val.into());

        let mut metas = PartiqlMetadata::new();
        metas.insert("vec value", expected_vec_val.clone().into());
        metas.insert("bool value", expected_bool_val.into());
        metas.insert("integer value", expected_int_val.into());
        metas.insert("float value", expected_float_val.into());
        metas.insert("string value", expected_str_val.into());
        metas.insert("map value", expected_map.clone().into());

        let vec_val = metas.vec_value("vec value").expect("vec meta value");
        let bool_val = metas.bool_value("bool value").expect("bool meta value");
        let int_val = metas.i32_value("integer value").expect("i32 meta value");
        let float_val = metas.f64_value("float value").expect("f64 meta value");
        let string_val = metas.string_value("string value").expect("string meta value");
        let map_val = metas.map_value("map value").expect("map meta value");

        assert_eq!(vec_val, expected_vec_val.clone());
        assert_eq!(bool_val, expected_bool_val.clone());
        assert_eq!(int_val, expected_int_val.clone());
        assert_eq!(float_val, expected_float_val.clone());
        assert_eq!(string_val, expected_str_val);
        assert_eq!(map_val, expected_map.clone());
```
  • Loading branch information
am357 committed Aug 16, 2024
1 parent 28fed58 commit 2676835
Show file tree
Hide file tree
Showing 8 changed files with 374 additions and 53 deletions.
5 changes: 2 additions & 3 deletions partiql-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ bench = false
indexmap = "2.2"
pretty = "0.12"
serde = { version = "1.*", features = ["derive"], optional = true }
rust_decimal = { version = "1.25.0", default-features = false, features = ["std"] }
smallvec = { version = "1.*" }
thiserror = "1.0"
dashmap = "6.0"

[features]
default = []
serde = [
"dep:serde",
"indexmap/serde",
"smallvec/serde",
"dashmap/serde"
"smallvec/serde"
]
1 change: 1 addition & 0 deletions partiql-common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![deny(rust_2018_idioms)]
#![deny(clippy::all)]
mod metadata;
pub mod node;
pub mod syntax;
366 changes: 366 additions & 0 deletions partiql-common/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,366 @@
use indexmap::map::Entry;
use indexmap::IndexMap;
use rust_decimal::Decimal;
use std::borrow::Borrow;
use std::fmt::Result;
use std::fmt::{Display, Formatter};
use std::hash::Hash;

#[derive(Debug, Clone, PartialEq)]
pub struct PartiqlMetadata<T>
where
T: Eq + Clone + Hash + Borrow<str>,
{
inner: IndexMap<T, PartiqlMetaValue<T>>,
}

impl<T> PartiqlMetadata<T>
where
T: Eq + Clone + Hash + Borrow<str>,
{
pub fn new() -> Self {

Check warning on line 21 in partiql-common/src/metadata.rs

View workflow job for this annotation

GitHub Actions / clippy

multiple associated items are never used

warning: multiple associated items are never used --> partiql-common/src/metadata.rs:21:12 | 17 | / impl<T> PartiqlMetadata<T> 18 | | where 19 | | T: Eq + Clone + Hash + Borrow<str>, | |_______________________________________- associated items in this implementation 20 | { 21 | pub fn new() -> Self { | ^^^ ... 27 | pub fn insert(&mut self, key: T, value: PartiqlMetaValue<T>) { | ^^^^^^ ... 31 | pub fn get(&self, key: &T) -> Option<&PartiqlMetaValue<T>> { | ^^^ ... 35 | pub fn get_mut(&mut self, key: &T) -> Option<&mut PartiqlMetaValue<T>> { | ^^^^^^^ ... 39 | pub fn contains_key(&self, key: &T) -> bool { | ^^^^^^^^^^^^ ... 43 | pub fn keys(&self) -> impl Iterator<Item = &T> { | ^^^^ ... 47 | pub fn values(&self) -> impl Iterator<Item = &PartiqlMetaValue<T>> { | ^^^^^^ ... 51 | pub fn values_mut(&mut self) -> impl Iterator<Item = &mut PartiqlMetaValue<T>> { | ^^^^^^^^^^ ... 55 | pub fn entry(&mut self, key: T) -> Entry<'_, T, PartiqlMetaValue<T>> { | ^^^^^ ... 59 | pub fn clear(&mut self) { | ^^^^^ ... 63 | pub fn remove(&mut self, key: &T) -> Option<PartiqlMetaValue<T>> { | ^^^^^^ ... 67 | pub fn len(&self) -> usize { | ^^^ ... 75 | pub fn iter_mut(&mut self) -> impl Iterator<Item = (&T, &mut PartiqlMetaValue<T>)> { | ^^^^^^^^ ... 79 | pub fn vec_value(&self, key: &str) -> Option<Vec<PartiqlMetaValue<T>>> { | ^^^^^^^^^ ... 88 | pub fn bool_value(&self, key: &str) -> Option<bool> { | ^^^^^^^^^^ ... 97 | pub fn f32_value(&self, key: &str) -> Option<f32> { | ^^^^^^^^^ ... 106 | pub fn f64_value(&self, key: &str) -> Option<f64> { | ^^^^^^^^^ ... 115 | pub fn decimal_value(&self, key: &str) -> Option<Decimal> { | ^^^^^^^^^^^^^ ... 124 | pub fn i32_value(&self, key: &str) -> Option<i32> { | ^^^^^^^^^ ... 133 | pub fn i64_value(&self, key: &str) -> Option<i64> { | ^^^^^^^^^ ... 142 | pub fn map_value(&self, key: &str) -> Option<PartiqlMetadata<T>> { | ^^^^^^^^^ ... 151 | pub fn string_value(&self, key: &str) -> Option<String> { | ^^^^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default
Self {
inner: IndexMap::new(),
}
}

pub fn insert(&mut self, key: T, value: PartiqlMetaValue<T>) {
self.inner.insert(key, value);
}

pub fn get(&self, key: &T) -> Option<&PartiqlMetaValue<T>> {
self.inner.get(key)
}

pub fn get_mut(&mut self, key: &T) -> Option<&mut PartiqlMetaValue<T>> {
self.inner.get_mut(key)
}

pub fn contains_key(&self, key: &T) -> bool {
self.inner.contains_key(key)
}

pub fn keys(&self) -> impl Iterator<Item = &T> {
self.inner.keys()
}

pub fn values(&self) -> impl Iterator<Item = &PartiqlMetaValue<T>> {
self.inner.values()
}

pub fn values_mut(&mut self) -> impl Iterator<Item = &mut PartiqlMetaValue<T>> {
self.inner.values_mut()
}

pub fn entry(&mut self, key: T) -> Entry<'_, T, PartiqlMetaValue<T>> {
self.inner.entry(key)
}

pub fn clear(&mut self) {
self.inner.clear();
}

pub fn remove(&mut self, key: &T) -> Option<PartiqlMetaValue<T>> {
self.inner.swap_remove(key)
}

pub fn len(&self) -> usize {
self.inner.len()
}

pub fn iter(&self) -> impl Iterator<Item = (&T, &PartiqlMetaValue<T>)> {
self.inner.iter()
}

pub fn iter_mut(&mut self) -> impl Iterator<Item = (&T, &mut PartiqlMetaValue<T>)> {
self.inner.iter_mut()
}

pub fn vec_value(&self, key: &str) -> Option<Vec<PartiqlMetaValue<T>>> {
let value = self.inner.get(key);
if let Some(PartiqlMetaValue::Array(v)) = value {
Some(v.clone())
} else {
None
}
}

pub fn bool_value(&self, key: &str) -> Option<bool> {
let value = self.inner.get(key);
if let Some(PartiqlMetaValue::Bool(v)) = value {
Some(*v)
} else {
None
}
}

pub fn f32_value(&self, key: &str) -> Option<f32> {
let value = self.inner.get(key);
if let Some(PartiqlMetaValue::Float32(v)) = value {
Some(*v)
} else {
None
}
}

pub fn f64_value(&self, key: &str) -> Option<f64> {
let value = self.inner.get(key);
if let Some(PartiqlMetaValue::Float64(v)) = value {
Some(*v)
} else {
None
}
}

pub fn decimal_value(&self, key: &str) -> Option<Decimal> {
let value = self.inner.get(key);
if let Some(PartiqlMetaValue::Decimal(v)) = value {
Some(*v)
} else {
None
}
}

pub fn i32_value(&self, key: &str) -> Option<i32> {
let value = self.inner.get(key);
if let Some(PartiqlMetaValue::Int32(v)) = value {
Some(*v)
} else {
None
}
}

pub fn i64_value(&self, key: &str) -> Option<i64> {
let value = self.inner.get(key);
if let Some(PartiqlMetaValue::Int64(v)) = value {
Some(*v)
} else {
None
}
}

pub fn map_value(&self, key: &str) -> Option<PartiqlMetadata<T>> {
let value = self.inner.get(key);
if let Some(PartiqlMetaValue::Map(v)) = value {
Some(v.clone())
} else {
None
}
}

pub fn string_value(&self, key: &str) -> Option<String> {
let value = self.inner.get(key);
if let Some(PartiqlMetaValue::String(v)) = value {
Some(v.clone())
} else {
None
}
}
}

impl<T> Default for PartiqlMetadata<T>
where
T: Eq + Clone + Hash + Borrow<str>,
{
fn default() -> Self {
Self {
inner: IndexMap::new(),
}
}
}

impl<T> FromIterator<(T, PartiqlMetaValue<T>)> for PartiqlMetadata<T>
where
T: Eq + Clone + Hash + Borrow<str>,
{
fn from_iter<I: IntoIterator<Item = (T, PartiqlMetaValue<T>)>>(iter: I) -> Self {
let inner = iter.into_iter().collect();
Self { inner }
}
}

impl<T> IntoIterator for PartiqlMetadata<T>
where
T: Eq + Clone + Hash + Borrow<str>,
{
type Item = (T, PartiqlMetaValue<T>);
type IntoIter = indexmap::map::IntoIter<T, PartiqlMetaValue<T>>;

fn into_iter(self) -> Self::IntoIter {
self.inner.into_iter()
}
}

#[derive(Clone, Debug, PartialEq)]
pub enum PartiqlMetaValue<T>
where
T: Eq + Clone + Hash + Borrow<str>,
{
Array(Vec<PartiqlMetaValue<T>>),
Bool(bool),
Float32(f32),

Check warning on line 201 in partiql-common/src/metadata.rs

View workflow job for this annotation

GitHub Actions / clippy

variants `Float32` and `Decimal` are never constructed

warning: variants `Float32` and `Decimal` are never constructed --> partiql-common/src/metadata.rs:201:5 | 195 | pub enum PartiqlMetaValue<T> | ---------------- variants in this enum ... 201 | Float32(f32), | ^^^^^^^ 202 | Float64(f64), 203 | Decimal(Decimal), | ^^^^^^^ | = note: `PartiqlMetaValue` has derived impls for the traits `Debug` and `Clone`, but these are intentionally ignored during dead code analysis
Float64(f64),
Decimal(Decimal),
Int32(i32),
Int64(i64),
Map(PartiqlMetadata<T>),
String(String),
}

impl<T> From<bool> for PartiqlMetaValue<T>
where
T: Eq + Clone + Hash + Borrow<str>,
{
fn from(value: bool) -> Self {
PartiqlMetaValue::Bool(value)
}
}

impl<T> From<i32> for PartiqlMetaValue<T>
where
T: Eq + Clone + Hash + Borrow<str>,
{
fn from(value: i32) -> Self {
PartiqlMetaValue::Int32(value)
}
}
impl<T> From<i64> for PartiqlMetaValue<T>
where
T: Eq + Clone + Hash + Borrow<str>,
{
fn from(value: i64) -> Self {
PartiqlMetaValue::Int64(value)
}
}

impl<T> From<f64> for PartiqlMetaValue<T>
where
T: Eq + Clone + Hash + Borrow<str>,
{
fn from(value: f64) -> Self {
PartiqlMetaValue::Float64(value)
}
}

impl<T> From<String> for PartiqlMetaValue<T>
where
T: Eq + Clone + Hash + Borrow<str>,
{
fn from(value: String) -> Self {
PartiqlMetaValue::String(value)
}
}
impl<T> From<&'static str> for PartiqlMetaValue<T>
where
T: Eq + Clone + Hash + Borrow<str>,
{
fn from(value: &'static str) -> Self {
PartiqlMetaValue::String(value.to_owned())
}
}

impl<T> From<Vec<PartiqlMetaValue<T>>> for PartiqlMetaValue<T>
where
T: Eq + Clone + Hash + Borrow<str>,
{
fn from(value: Vec<PartiqlMetaValue<T>>) -> Self {
PartiqlMetaValue::Array(value)
}
}

impl<T> From<&[PartiqlMetaValue<T>]> for PartiqlMetaValue<T>
where
T: Eq + Clone + Hash + Borrow<str>,
{
fn from(slice: &[PartiqlMetaValue<T>]) -> Self {
PartiqlMetaValue::Array(slice.to_vec())
}
}

impl<T> From<PartiqlMetadata<T>> for PartiqlMetaValue<T>
where
T: Eq + Clone + Hash + Borrow<str>,
{
fn from(value: PartiqlMetadata<T>) -> Self {
PartiqlMetaValue::Map(value)
}
}

impl<T> Display for PartiqlMetaValue<T>
where
T: Eq + Hash + Display + Clone + Borrow<str>,
{
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match self {
PartiqlMetaValue::Array(arr) => {
write!(f, "[")?;
for (idx, item) in arr.iter().enumerate() {
if idx > 0 {
write!(f, ", ")?;
}
write!(f, "{}", item)?;
}
write!(f, "]")
}
PartiqlMetaValue::Bool(v) => write!(f, "{}", v),
PartiqlMetaValue::Decimal(v) => write!(f, "{}", v),
PartiqlMetaValue::Float64(v) => write!(f, "{}", v),
PartiqlMetaValue::Float32(v) => write!(f, "{}", v),
PartiqlMetaValue::Int32(v) => write!(f, "{}", v),
PartiqlMetaValue::Int64(v) => write!(f, "{}", v),
PartiqlMetaValue::Map(map) => {
write!(f, "{{")?;
for (t, v) in map.iter() {
write!(f, "{}: {} , ", t, v)?;
}
write!(f, "}}")
}
PartiqlMetaValue::String(v) => write!(f, "{}", v),
}
}
}

#[cfg(test)]
mod tests {
use crate::metadata::{PartiqlMetaValue, PartiqlMetadata};

#[test]
fn test_metadata() {
let foo_val = PartiqlMetaValue::String("foo".to_string());
let i64_val = PartiqlMetaValue::Int64(2);

let expected_vec_val = vec![foo_val, i64_val];
let expected_bool_val = true;
let expected_int_val = 2;
let expected_float_val = 2.5;
let expected_str_val = "foo";

let mut expected_map = PartiqlMetadata::new();
expected_map.insert("bool value", expected_bool_val.into());
expected_map.insert("integer value", expected_int_val.into());

let mut metas = PartiqlMetadata::new();
metas.insert("vec value", expected_vec_val.clone().into());
metas.insert("bool value", expected_bool_val.into());
metas.insert("integer value", expected_int_val.into());
metas.insert("float value", expected_float_val.into());
metas.insert("string value", expected_str_val.into());
metas.insert("map value", expected_map.clone().into());

let vec_val = metas.vec_value("vec value").expect("vec meta value");
let bool_val = metas.bool_value("bool value").expect("bool meta value");
let int_val = metas.i32_value("integer value").expect("i32 meta value");
let float_val = metas.f64_value("float value").expect("f64 meta value");
let string_val = metas
.string_value("string value")
.expect("string meta value");
let map_val = metas.map_value("map value").expect("map meta value");

assert_eq!(vec_val, expected_vec_val.clone());
assert_eq!(bool_val, expected_bool_val.clone());
assert_eq!(int_val, expected_int_val.clone());
assert_eq!(float_val, expected_float_val.clone());
assert_eq!(string_val, expected_str_val);
assert_eq!(map_val, expected_map.clone());
}
}
Loading

0 comments on commit 2676835

Please sign in to comment.