Skip to content

Commit bd7716d

Browse files
authored
Add macro_rules for partiql-types and remove partiql from value macro_rules (#398)
Adds first iteration of PartiqlType macro_rules and renames the existing value macro_rules to make shorter.
1 parent fd60ab3 commit bd7716d

File tree

17 files changed

+466
-399
lines changed

17 files changed

+466
-399
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased]
99
### Changed
1010
- *BREAKING:* partiql-logical-planner: moves `NameResolver` to `partiql-ast-passes`
11+
- *BREAKING:* partiql-values: removes `partiql` from value macro_rules; e.g. `partiql_bag` renames to `bag`.
1112

1213
### Added
1314
- Add ability for partiql-extension-ion extension encoding/decoding of `Value` to/from Ion `Element`

extension/partiql-extension-ion-functions/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ mod tests {
158158
use partiql_catalog::{Catalog, Extension, PartiqlCatalog};
159159
use partiql_eval::env::basic::MapBindings;
160160
use partiql_parser::{Parsed, ParserResult};
161-
use partiql_value::{partiql_bag, partiql_tuple, Value};
161+
use partiql_value::{bag, tuple, Value};
162162

163163
#[track_caller]
164164
#[inline]
@@ -217,12 +217,12 @@ mod tests {
217217

218218
#[test]
219219
fn custom_ion_scan() {
220-
let value = partiql_bag![
221-
partiql_tuple![("Program", "p1"), ("Operation", "get")],
222-
partiql_tuple![("Program", "p1"), ("Operation", "put")],
223-
partiql_tuple![("Program", "p2"), ("Operation", "get")],
224-
partiql_tuple![("Program", "p2"), ("Operation", "put")],
225-
partiql_tuple![("Program", "p3"), ("Operation", "update")],
220+
let value = bag![
221+
tuple![("Program", "p1"), ("Operation", "get")],
222+
tuple![("Program", "p1"), ("Operation", "put")],
223+
tuple![("Program", "p2"), ("Operation", "get")],
224+
tuple![("Program", "p2"), ("Operation", "put")],
225+
tuple![("Program", "p3"), ("Operation", "update")],
226226
]
227227
.into();
228228

extension/partiql-extension-ion/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod tests {
1616
use ion_rs::types::{Bytes, Sequence, Struct};
1717
use ion_rs::{Decimal, Int, IonType, Str, Timestamp};
1818

19-
use partiql_value::{partiql_bag, partiql_list, partiql_tuple, DateTime, Value};
19+
use partiql_value::{bag, list, tuple, DateTime, Value};
2020
use rust_decimal_macros::dec;
2121
use std::num::NonZeroU8;
2222

@@ -221,7 +221,7 @@ mod tests {
221221
ion_rs::element::Value::Int(Int::I64(2)),
222222
ion_rs::element::Value::String(Str::from("3")),
223223
])),
224-
partiql_list![1, 2, "3"],
224+
list![1, 2, "3"],
225225
);
226226

227227
// struct
@@ -232,7 +232,7 @@ mod tests {
232232
.with_field("k", ion_rs::element::List(Sequence::new([1, 2, 3])))
233233
.build(),
234234
),
235-
partiql_tuple![("k", partiql_list![1, 2, 3])],
235+
tuple![("k", list![1, 2, 3])],
236236
);
237237
}
238238

@@ -377,7 +377,7 @@ mod tests {
377377
ion_rs::element::Value::Int(Int::I64(2)),
378378
ion_rs::element::Value::String(Str::from("3")),
379379
])),
380-
partiql_list![1, 2, "3"],
380+
list![1, 2, "3"],
381381
);
382382

383383
// bag
@@ -391,7 +391,7 @@ mod tests {
391391
ion_rs::element::Value::Null(IonType::Null).with_annotations(["$missing"]),
392392
]))
393393
.with_annotations(["$bag"]),
394-
partiql_bag![1, 2, "3", Value::Null, Value::Missing],
394+
bag![1, 2, "3", Value::Null, Value::Missing],
395395
);
396396

397397
// struct
@@ -402,7 +402,7 @@ mod tests {
402402
.with_field("k", ion_rs::element::List(Sequence::new::<Element, _>([])))
403403
.build(),
404404
),
405-
partiql_tuple![("k", partiql_list![])],
405+
tuple![("k", list![])],
406406
);
407407
assert_partiql_encoded_ion(
408408
"{\"k\": [1,2,3]}",
@@ -411,7 +411,7 @@ mod tests {
411411
.with_field("k", ion_rs::element::List(Sequence::new([1, 2, 3])))
412412
.build(),
413413
),
414-
partiql_tuple![("k", partiql_list![1, 2, 3])],
414+
tuple![("k", list![1, 2, 3])],
415415
);
416416
}
417417
}

partiql-ast-passes/src/partiql_typer.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ use partiql_types::{ArrayType, BagType, PartiqlType, StructType, TypeKind};
88

99
#[derive(Debug, Clone)]
1010
#[allow(dead_code)]
11-
pub struct AstStaticTyper<'c> {
11+
pub struct AstPartiqlTyper<'c> {
1212
id_stack: Vec<NodeId>,
1313
container_stack: Vec<Vec<PartiqlType>>,
1414
errors: Vec<AstTransformError>,
1515
type_map: AstTypeMap<PartiqlType>,
1616
catalog: &'c dyn Catalog,
1717
}
1818

19-
impl<'c> AstStaticTyper<'c> {
19+
impl<'c> AstPartiqlTyper<'c> {
2020
pub fn new(catalog: &'c dyn Catalog) -> Self {
21-
AstStaticTyper {
21+
AstPartiqlTyper {
2222
id_stack: Default::default(),
2323
container_stack: Default::default(),
2424
errors: Default::default(),
@@ -47,7 +47,7 @@ impl<'c> AstStaticTyper<'c> {
4747
}
4848
}
4949

50-
impl<'c, 'ast> Visitor<'ast> for AstStaticTyper<'c> {
50+
impl<'c, 'ast> Visitor<'ast> for AstPartiqlTyper<'c> {
5151
fn enter_ast_node(&mut self, id: NodeId) -> Traverse {
5252
self.id_stack.push(id);
5353
Traverse::Continue
@@ -116,9 +116,9 @@ impl<'c, 'ast> Visitor<'ast> for AstStaticTyper<'c> {
116116
Lit::NationalCharStringLit(_) => TypeKind::String,
117117
Lit::BitStringLit(_) => todo!(),
118118
Lit::HexStringLit(_) => todo!(),
119-
Lit::StructLit(_) => TypeKind::Struct(StructType::unconstrained()),
120-
Lit::ListLit(_) => TypeKind::Array(ArrayType::array()),
121-
Lit::BagLit(_) => TypeKind::Bag(BagType::bag()),
119+
Lit::StructLit(_) => TypeKind::Struct(StructType::new_any()),
120+
Lit::ListLit(_) => TypeKind::Array(ArrayType::new_any()),
121+
Lit::BagLit(_) => TypeKind::Bag(BagType::new_any()),
122122
Lit::TypedLit(_, _) => todo!(),
123123
};
124124

@@ -161,7 +161,7 @@ impl<'c, 'ast> Visitor<'ast> for AstStaticTyper<'c> {
161161
}
162162
}
163163

164-
let ty = PartiqlType::new_struct(StructType::unconstrained());
164+
let ty = PartiqlType::new_struct(StructType::new_any());
165165
self.type_map.insert(id, ty.clone());
166166

167167
if let Some(c) = self.container_stack.last_mut() {
@@ -184,7 +184,7 @@ impl<'c, 'ast> Visitor<'ast> for AstStaticTyper<'c> {
184184
self.container_stack.pop();
185185

186186
let id = *self.current_node();
187-
let ty = PartiqlType::new_bag(BagType::bag());
187+
let ty = PartiqlType::new_bag(BagType::new_any());
188188

189189
self.type_map.insert(id, ty.clone());
190190
if let Some(s) = self.container_stack.last_mut() {
@@ -206,7 +206,7 @@ impl<'c, 'ast> Visitor<'ast> for AstStaticTyper<'c> {
206206
self.container_stack.pop();
207207

208208
let id = *self.current_node();
209-
let ty = PartiqlType::new_array(ArrayType::array());
209+
let ty = PartiqlType::new_array(ArrayType::new_any());
210210

211211
self.type_map.insert(id, ty.clone());
212212
if let Some(s) = self.container_stack.last_mut() {
@@ -221,8 +221,8 @@ mod tests {
221221
use super::*;
222222
use assert_matches::assert_matches;
223223
use partiql_ast::ast;
224-
use partiql_catalog::PartiqlCatalog;
225-
use partiql_types::{PartiqlType, TypeKind};
224+
use partiql_catalog::{PartiqlCatalog, TypeEnvEntry};
225+
use partiql_types::{PartiqlType, StructConstraint, StructField, TypeKind};
226226

227227
#[test]
228228
fn simple_test() {
@@ -244,22 +244,24 @@ mod tests {
244244

245245
#[test]
246246
fn simple_err_test() {
247-
assert!(type_statement("{'a': 1, a.b: 3}").is_err());
247+
assert!(type_statement("{'a': 1, a.b: 3}", &PartiqlCatalog::default()).is_err());
248248
}
249249

250250
fn run_literal_test(q: &str) -> TypeKind {
251-
let out = type_statement(q).expect("type map");
251+
let out = type_statement(q, &PartiqlCatalog::default()).expect("type map");
252252
let values: Vec<&PartiqlType> = out.values().collect();
253253
values.last().unwrap().kind().clone()
254254
}
255255

256-
fn type_statement(q: &str) -> Result<AstTypeMap<PartiqlType>, AstTransformationError> {
256+
fn type_statement(
257+
q: &str,
258+
catalog: &dyn Catalog,
259+
) -> Result<AstTypeMap<PartiqlType>, AstTransformationError> {
257260
let parsed = partiql_parser::Parser::default()
258261
.parse(q)
259262
.expect("Expect successful parse");
260263

261-
let catalog = PartiqlCatalog::default();
262-
let typer = AstStaticTyper::new(&catalog);
264+
let typer = AstPartiqlTyper::new(catalog);
263265
if let ast::Expr::Query(q) = parsed.ast.as_ref() {
264266
typer.type_nodes(&q)
265267
} else {

partiql-catalog/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ pub struct TypeEnvEntry<'a> {
118118
ty: PartiqlType,
119119
}
120120

121+
impl<'a> TypeEnvEntry<'a> {
122+
pub fn new(name: &str, aliases: &[&'a str], ty: PartiqlType) -> Self {
123+
TypeEnvEntry {
124+
name: UniCase::from(name.to_string()),
125+
aliases: aliases.to_vec(),
126+
ty,
127+
}
128+
}
129+
}
130+
121131
#[derive(Debug)]
122132
pub struct TypeEntry {
123133
id: ObjectId,

partiql-conformance-tests/tests/test_value.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn parse_test_value_str(contents: &str) -> Value {
3737
mod tests {
3838
use super::parse_test_value_str;
3939

40-
use partiql_value::{partiql_bag, partiql_list, partiql_tuple, Value};
40+
use partiql_value::{bag, list, tuple, Value};
4141

4242
#[track_caller]
4343
fn parse(test: &str, expected: Value) {
@@ -64,7 +64,7 @@ mod tests {
6464
s: 1
6565
}
6666
]";
67-
let expected = Value::from(partiql_bag![partiql_tuple![
67+
let expected = Value::from(bag![tuple![
6868
("f", 1),
6969
("d", Value::Real(2.0.into())),
7070
("s", 1)
@@ -79,7 +79,7 @@ mod tests {
7979
sensor: 1,
8080
reading: 42
8181
}",
82-
Value::Tuple(Box::new(partiql_tuple![("sensor", 1), ("reading", 42)])),
82+
Value::Tuple(Box::new(tuple![("sensor", 1), ("reading", 42)])),
8383
);
8484
}
8585

@@ -110,17 +110,17 @@ mod tests {
110110
}
111111
]
112112
}",
113-
Value::Tuple(Box::new(partiql_tuple![
113+
Value::Tuple(Box::new(tuple![
114114
(
115115
"sensors",
116-
partiql_list![partiql_tuple![("sensor", 1)], partiql_tuple![("sensor", 2)]]
116+
list![tuple![("sensor", 1)], tuple![("sensor", 2)]]
117117
),
118118
(
119119
"logs",
120-
partiql_list![
121-
partiql_tuple![("sensor", 1), ("co", rust_decimal::Decimal::new(4, 1))],
122-
partiql_tuple![("sensor", 1), ("co", rust_decimal::Decimal::new(2, 1))],
123-
partiql_tuple![("sensor", 2), ("co", rust_decimal::Decimal::new(3, 1))]
120+
list![
121+
tuple![("sensor", 1), ("co", rust_decimal::Decimal::new(4, 1))],
122+
tuple![("sensor", 1), ("co", rust_decimal::Decimal::new(2, 1))],
123+
tuple![("sensor", 2), ("co", rust_decimal::Decimal::new(3, 1))]
124124
]
125125
)
126126
])),
@@ -136,7 +136,7 @@ mod tests {
136136
s: 1
137137
}
138138
]";
139-
let expected = Value::from(partiql_list![partiql_tuple![
139+
let expected = Value::from(list![tuple![
140140
("f", 1),
141141
("d", Value::Real(2.0.into())),
142142
("s", 1)

partiql-eval/benches/bench_eval.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,36 @@ use partiql_eval::plan;
1010
use partiql_logical as logical;
1111
use partiql_logical::BindingsOp::{Project, ProjectAll};
1212
use partiql_logical::{BinaryOp, BindingsOp, JoinKind, LogicalPlan, PathComponent, ValueExpr};
13-
use partiql_value::{partiql_bag, partiql_list, partiql_tuple, BindingsName, Value};
13+
use partiql_value::{bag, list, tuple, BindingsName, Value};
1414

1515
fn data() -> MapBindings<Value> {
16-
let hr = partiql_tuple![(
16+
let hr = tuple![(
1717
"employeesNestScalars",
18-
partiql_bag![
19-
partiql_tuple![
18+
bag![
19+
tuple![
2020
("id", 3),
2121
("name", "Bob Smith"),
2222
("title", Value::Null),
2323
(
2424
"projects",
25-
partiql_list![
25+
list![
2626
"AWS Redshift Spectrum querying",
2727
"AWS Redshift security",
2828
"AWS Aurora security",
2929
]
3030
),
3131
],
32-
partiql_tuple![
32+
tuple![
3333
("id", 4),
3434
("name", "Susan Smith"),
3535
("title", "Dev Mgr"),
36-
("projects", partiql_list![]),
36+
("projects", list![]),
3737
],
38-
partiql_tuple![
38+
tuple![
3939
("id", 6),
4040
("name", "Jane Smith"),
4141
("title", "Software Eng 2"),
42-
("projects", partiql_list!["AWS Redshift security"]),
42+
("projects", list!["AWS Redshift security"]),
4343
],
4444
]
4545
)];
@@ -50,14 +50,14 @@ fn data() -> MapBindings<Value> {
5050
}
5151

5252
fn join_data() -> MapBindings<Value> {
53-
let customers = partiql_list![
54-
partiql_tuple![("id", 5), ("name", "Joe")],
55-
partiql_tuple![("id", 7), ("name", "Mary")],
53+
let customers = list![
54+
tuple![("id", 5), ("name", "Joe")],
55+
tuple![("id", 7), ("name", "Mary")],
5656
];
5757

58-
let orders = partiql_list![
59-
partiql_tuple![("custId", 7), ("productId", 101)],
60-
partiql_tuple![("custId", 7), ("productId", 523)],
58+
let orders = list![
59+
tuple![("custId", 7), ("productId", 101)],
60+
tuple![("custId", 7), ("productId", 523)],
6161
];
6262

6363
let mut bindings = MapBindings::default();

0 commit comments

Comments
 (0)