Skip to content

Commit 8042ef9

Browse files
authored
Add partiql_tuple! macro (#198)
1 parent 337f912 commit 8042ef9

File tree

3 files changed

+90
-117
lines changed

3 files changed

+90
-117
lines changed

partiql-eval/benches/bench_eval.rs

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,44 @@ use partiql_eval::eval::{
55
BasicContext, EvalFrom, EvalOutputAccumulator, EvalPath, EvalVarRef, Evaluable, Output,
66
PathComponent,
77
};
8-
use partiql_value::{partiql_bag, partiql_list, Bag, BindingsName, List, Tuple, Value};
8+
use partiql_value::{
9+
partiql_bag, partiql_list, partiql_tuple, Bag, BindingsName, List, Tuple, Value,
10+
};
911
use std::cell::RefCell;
1012
use std::rc::Rc;
1113
use std::time::Duration;
1214

1315
fn data() -> MapBindings<Value> {
14-
let employees = partiql_bag![
15-
Tuple::from([
16-
("id", 3.into()),
17-
("name", "Bob Smith".into()),
18-
("title", Value::Null),
19-
(
20-
"projects",
21-
partiql_list![
22-
"AWS Redshift Spectrum querying".into(),
23-
"AWS Redshift security".into(),
24-
"AWS Aurora security".into()
25-
]
26-
.into()
27-
),
28-
])
29-
.into(),
30-
Tuple::from([
31-
("id", 4.into()),
32-
("name", "Susan Smith".into()),
33-
("title", "Dev Mgr".into()),
34-
("projects", partiql_list![].into()),
35-
])
36-
.into(),
37-
Tuple::from([
38-
("id", 6.into()),
39-
("name", "Jane Smith".into()),
40-
("title", "Software Eng 2".into()),
41-
(
42-
"projects",
43-
partiql_list!["AWS Redshift security".into()].into()
44-
),
45-
])
46-
.into(),
47-
];
48-
let hr = Tuple::from([("employeesNestScalars", Value::from(employees))]);
16+
let hr = partiql_tuple![(
17+
"employeesNestScalars",
18+
partiql_bag![
19+
partiql_tuple![
20+
("id", 3),
21+
("name", "Bob Smith"),
22+
("title", Value::Null),
23+
(
24+
"projects",
25+
partiql_list![
26+
"AWS Redshift Spectrum querying",
27+
"AWS Redshift security",
28+
"AWS Aurora security",
29+
]
30+
),
31+
],
32+
partiql_tuple![
33+
("id", 4),
34+
("name", "Susan Smith"),
35+
("title", "Dev Mgr"),
36+
("projects", partiql_list![]),
37+
],
38+
partiql_tuple![
39+
("id", 6),
40+
("name", "Jane Smith"),
41+
("title", "Software Eng 2"),
42+
("projects", partiql_list!["AWS Redshift security"]),
43+
],
44+
]
45+
)];
4946

5047
let mut p0: MapBindings<Value> = MapBindings::default();
5148
p0.insert("hr", hr.into());

partiql-eval/src/lib.rs

Lines changed: 21 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ mod tests {
1111
use crate::env::basic::MapBindings;
1212
use crate::plan;
1313
use partiql_logical::{BindingsExpr, PathComponent, ValueExpr};
14-
use partiql_value::{partiql_bag, partiql_list, Bag, BindingsName, List, Tuple, Value};
14+
use partiql_value::{
15+
partiql_bag, partiql_list, partiql_tuple, Bag, BindingsName, List, Tuple, Value,
16+
};
1517
use std::cell::RefCell;
1618
use std::collections::HashMap;
1719
use std::rc::Rc;
@@ -165,8 +167,8 @@ mod tests {
165167

166168
fn some_ordered_table() -> List {
167169
partiql_list![
168-
Tuple::from([("a", Value::from(0)), ("b", Value::from(0))]).into(),
169-
Tuple::from([("a", 1.into()), ("b", 1.into())]).into(),
170+
partiql_tuple![("a", 0), ("b", 0)],
171+
partiql_tuple![("a", 1), ("b", 1)],
170172
]
171173
}
172174

@@ -198,16 +200,8 @@ mod tests {
198200
println!("{:?}", &output.borrow().output);
199201
// <<{ x: { b: 0, a: 0 } }, { x: { b: 1, a: 1 } }>>
200202
let expected = partiql_bag![
201-
Tuple::from([(
202-
"x",
203-
Tuple::from([("a", Value::Integer(0)), ("b", 0.into())]).into()
204-
),])
205-
.into(),
206-
Tuple::from([(
207-
"x",
208-
Tuple::from([("a", Value::Integer(1)), ("b", 1.into())]).into()
209-
),])
210-
.into()
203+
partiql_tuple![("x", partiql_tuple![("a", 0), ("b", 0)]),],
204+
partiql_tuple![("x", partiql_tuple![("a", 1), ("b", 1)]),],
211205
];
212206
assert_eq!(&expected, &output.borrow().output);
213207
}
@@ -237,22 +231,8 @@ mod tests {
237231
println!("{:?}", &output.borrow().output);
238232
// <<{ y: 0, x: { b: 0, a: 0 } }, { x: { b: 1, a: 1 }, y: 1 }>>
239233
let expected = partiql_bag![
240-
Tuple::from([
241-
(
242-
"x",
243-
Tuple::from([("a", Value::Integer(0)), ("b", 0.into())]).into()
244-
),
245-
("y", Value::Integer(0))
246-
])
247-
.into(),
248-
Tuple::from([
249-
(
250-
"x",
251-
Tuple::from([("a", Value::Integer(1)), ("b", 1.into())]).into()
252-
),
253-
("y", value::Value::Integer(1))
254-
])
255-
.into()
234+
partiql_tuple![("x", partiql_tuple![("a", 0), ("b", 0)]), ("y", 0)],
235+
partiql_tuple![("x", partiql_tuple![("a", 1), ("b", 1)]), ("y", 1)],
256236
];
257237
assert_eq!(&expected, &output.borrow().output);
258238
}
@@ -282,22 +262,14 @@ mod tests {
282262
println!("{:?}", &output.borrow().output);
283263
// <<{ y: MISSING, x: { b: 0, a: 0 } }, { x: { b: 1, a: 1 }, y: MISSING }>>
284264
let expected = partiql_bag![
285-
Tuple::from([
286-
(
287-
"x",
288-
Tuple::from([("a", Value::Integer(0)), ("b", 0.into())]).into()
289-
),
265+
partiql_tuple![
266+
("x", partiql_tuple![("a", 0), ("b", 0)]),
290267
("y", value::Value::Missing)
291-
])
292-
.into(),
293-
Tuple::from([
294-
(
295-
"x",
296-
Tuple::from([("a", Value::Integer(1)), ("b", 1.into())]).into()
297-
),
268+
],
269+
partiql_tuple![
270+
("x", partiql_tuple![("a", 1), ("b", 1)]),
298271
("y", value::Value::Missing)
299-
])
300-
.into()
272+
],
301273
];
302274
assert_eq!(&expected, &output.borrow().output);
303275
}
@@ -326,7 +298,7 @@ mod tests {
326298
from.evaluate(&ctx);
327299

328300
println!("{:?}", &output.borrow().output);
329-
let expected = partiql_bag![Tuple::from([("x", 0.into())]).into()];
301+
let expected = partiql_bag![partiql_tuple![("x", 0)]];
330302
assert_eq!(&expected, &output.borrow().output);
331303
}
332304

@@ -354,7 +326,7 @@ mod tests {
354326
from.evaluate(&ctx);
355327

356328
println!("{:?}", &output.borrow().output);
357-
let expected = partiql_bag![Tuple::from([("x", value::Value::Missing)]).into()];
329+
let expected = partiql_bag![partiql_tuple![("x", value::Value::Missing)]];
358330
assert_eq!(&expected, &output.borrow().output);
359331
}
360332
}
@@ -365,7 +337,7 @@ mod tests {
365337
use partiql_value::{partiql_bag, BindingsName, Tuple};
366338

367339
fn just_a_tuple() -> Tuple {
368-
Tuple::from([("amzn", Value::from(840.05)), ("tdc", Value::from(31.06))])
340+
partiql_tuple![("amzn", 840.05), ("tdc", 31.06)]
369341
}
370342

371343
// Spec 5.2
@@ -392,16 +364,8 @@ mod tests {
392364

393365
println!("{:?}", &output.borrow().output);
394366
let expected = partiql_bag![
395-
Tuple::from([
396-
("symbol", "tdc".into()),
397-
("price", Value::Real(31.06.into()))
398-
])
399-
.into(),
400-
Tuple::from([
401-
("symbol", "amzn".into()),
402-
("price", Value::Real(840.05.into()))
403-
])
404-
.into(),
367+
partiql_tuple![("symbol", "tdc"), ("price", 31.06)],
368+
partiql_tuple![("symbol", "amzn"), ("price", 840.05)],
405369
];
406370
assert_eq!(&expected, &output.borrow().output);
407371
}
@@ -429,8 +393,7 @@ mod tests {
429393
unpivot.evaluate(&ctx);
430394

431395
println!("{:?}", &output.borrow().output);
432-
let expected =
433-
partiql_bag![Tuple::from([("x", Value::Integer(1)), ("y", "_1".into())]).into()];
396+
let expected = partiql_bag![partiql_tuple![("x", 1), ("y", "_1")]];
434397
assert_eq!(&expected, &output.borrow().output);
435398
}
436399
}

partiql-value/src/lib.rs

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,16 @@ impl From<Bag> for List {
222222

223223
#[macro_export]
224224
macro_rules! partiql_list {
225-
() => (
226-
List::from(vec![])
227-
);
228-
($elem:expr; $n:expr) => (
229-
List::from(vec![$elem, $n])
230-
);
231-
($($x:expr),+ $(,)?) => (
232-
List::from(vec![$($x),+])
233-
);
234-
}
225+
() => (
226+
List::from(vec![])
227+
);
228+
($elem:expr; $n:expr) => (
229+
List::from(vec![Value::from($elem), $n])
230+
);
231+
($($x:expr),+ $(,)?) => (
232+
List::from(vec![$(Value::from($x)),+])
233+
);
234+
}
235235

236236
impl IntoIterator for List {
237237
type Item = Value;
@@ -342,16 +342,16 @@ impl From<List> for Bag {
342342

343343
#[macro_export]
344344
macro_rules! partiql_bag {
345-
() => (
346-
Bag::from(vec![])
347-
);
348-
($elem:expr; $n:expr) => (
349-
Bag::from(vec![$elem, $n])
350-
);
351-
($($x:expr),+ $(,)?) => (
352-
Bag::from(vec![$($x),+])
353-
);
354-
}
345+
() => (
346+
Bag::from(vec![])
347+
);
348+
($elem:expr; $n:expr) => (
349+
Bag::from(vec![Value::from($elem), $n])
350+
);
351+
($($x:expr),+ $(,)?) => (
352+
Bag::from(vec![$(Value::from($x)),+])
353+
);
354+
}
355355

356356
impl IntoIterator for Bag {
357357
type Item = Value;
@@ -432,11 +432,14 @@ impl Hash for Bag {
432432
#[derive(Default, Eq, Clone)]
433433
pub struct Tuple(pub HashMap<String, Value>);
434434

435-
impl<const N: usize> From<[(&str, Value); N]> for Tuple {
435+
impl<const N: usize, T> From<[(&str, T); N]> for Tuple
436+
where
437+
T: Into<Value>,
438+
{
436439
#[inline]
437-
fn from(arr: [(&str, Value); N]) -> Self {
440+
fn from(arr: [(&str, T); N]) -> Self {
438441
Tuple(HashMap::from_iter(
439-
arr.into_iter().map(|(k, v)| (k.to_string(), v)),
442+
arr.into_iter().map(|(k, v)| (k.to_string(), v.into())),
440443
))
441444
}
442445
}
@@ -448,6 +451,16 @@ impl From<Tuple> for Value {
448451
}
449452
}
450453

454+
#[macro_export]
455+
macro_rules! partiql_tuple {
456+
() => (
457+
Tuple::from(vec![])
458+
);
459+
($(($x:expr, $y:expr)),+ $(,)?) => (
460+
Tuple::from([$(($x, Value::from($y))),+])
461+
);
462+
}
463+
451464
impl Debug for Tuple {
452465
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
453466
let mut dbg = f.debug_struct("");

0 commit comments

Comments
 (0)