Skip to content

Commit

Permalink
Add macro_rules for partiql-types and remove partiql from value m…
Browse files Browse the repository at this point in the history
…acro_rules (#398)

Adds first iteration of PartiqlType macro_rules and renames the existing
value macro_rules to make shorter.
  • Loading branch information
am357 authored Jun 15, 2023
1 parent fd60ab3 commit bd7716d
Show file tree
Hide file tree
Showing 17 changed files with 466 additions and 399 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Changed
- *BREAKING:* partiql-logical-planner: moves `NameResolver` to `partiql-ast-passes`
- *BREAKING:* partiql-values: removes `partiql` from value macro_rules; e.g. `partiql_bag` renames to `bag`.

### Added
- Add ability for partiql-extension-ion extension encoding/decoding of `Value` to/from Ion `Element`
Expand Down
14 changes: 7 additions & 7 deletions extension/partiql-extension-ion-functions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ mod tests {
use partiql_catalog::{Catalog, Extension, PartiqlCatalog};
use partiql_eval::env::basic::MapBindings;
use partiql_parser::{Parsed, ParserResult};
use partiql_value::{partiql_bag, partiql_tuple, Value};
use partiql_value::{bag, tuple, Value};

#[track_caller]
#[inline]
Expand Down Expand Up @@ -217,12 +217,12 @@ mod tests {

#[test]
fn custom_ion_scan() {
let value = partiql_bag![
partiql_tuple![("Program", "p1"), ("Operation", "get")],
partiql_tuple![("Program", "p1"), ("Operation", "put")],
partiql_tuple![("Program", "p2"), ("Operation", "get")],
partiql_tuple![("Program", "p2"), ("Operation", "put")],
partiql_tuple![("Program", "p3"), ("Operation", "update")],
let value = bag![
tuple![("Program", "p1"), ("Operation", "get")],
tuple![("Program", "p1"), ("Operation", "put")],
tuple![("Program", "p2"), ("Operation", "get")],
tuple![("Program", "p2"), ("Operation", "put")],
tuple![("Program", "p3"), ("Operation", "update")],
]
.into();

Expand Down
14 changes: 7 additions & 7 deletions extension/partiql-extension-ion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod tests {
use ion_rs::types::{Bytes, Sequence, Struct};
use ion_rs::{Decimal, Int, IonType, Str, Timestamp};

use partiql_value::{partiql_bag, partiql_list, partiql_tuple, DateTime, Value};
use partiql_value::{bag, list, tuple, DateTime, Value};
use rust_decimal_macros::dec;
use std::num::NonZeroU8;

Expand Down Expand Up @@ -221,7 +221,7 @@ mod tests {
ion_rs::element::Value::Int(Int::I64(2)),
ion_rs::element::Value::String(Str::from("3")),
])),
partiql_list![1, 2, "3"],
list![1, 2, "3"],
);

// struct
Expand All @@ -232,7 +232,7 @@ mod tests {
.with_field("k", ion_rs::element::List(Sequence::new([1, 2, 3])))
.build(),
),
partiql_tuple![("k", partiql_list![1, 2, 3])],
tuple![("k", list![1, 2, 3])],
);
}

Expand Down Expand Up @@ -377,7 +377,7 @@ mod tests {
ion_rs::element::Value::Int(Int::I64(2)),
ion_rs::element::Value::String(Str::from("3")),
])),
partiql_list![1, 2, "3"],
list![1, 2, "3"],
);

// bag
Expand All @@ -391,7 +391,7 @@ mod tests {
ion_rs::element::Value::Null(IonType::Null).with_annotations(["$missing"]),
]))
.with_annotations(["$bag"]),
partiql_bag![1, 2, "3", Value::Null, Value::Missing],
bag![1, 2, "3", Value::Null, Value::Missing],
);

// struct
Expand All @@ -402,7 +402,7 @@ mod tests {
.with_field("k", ion_rs::element::List(Sequence::new::<Element, _>([])))
.build(),
),
partiql_tuple![("k", partiql_list![])],
tuple![("k", list![])],
);
assert_partiql_encoded_ion(
"{\"k\": [1,2,3]}",
Expand All @@ -411,7 +411,7 @@ mod tests {
.with_field("k", ion_rs::element::List(Sequence::new([1, 2, 3])))
.build(),
),
partiql_tuple![("k", partiql_list![1, 2, 3])],
tuple![("k", list![1, 2, 3])],
);
}
}
36 changes: 19 additions & 17 deletions partiql-ast-passes/src/partiql_typer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ use partiql_types::{ArrayType, BagType, PartiqlType, StructType, TypeKind};

#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct AstStaticTyper<'c> {
pub struct AstPartiqlTyper<'c> {
id_stack: Vec<NodeId>,
container_stack: Vec<Vec<PartiqlType>>,
errors: Vec<AstTransformError>,
type_map: AstTypeMap<PartiqlType>,
catalog: &'c dyn Catalog,
}

impl<'c> AstStaticTyper<'c> {
impl<'c> AstPartiqlTyper<'c> {
pub fn new(catalog: &'c dyn Catalog) -> Self {
AstStaticTyper {
AstPartiqlTyper {
id_stack: Default::default(),
container_stack: Default::default(),
errors: Default::default(),
Expand Down Expand Up @@ -47,7 +47,7 @@ impl<'c> AstStaticTyper<'c> {
}
}

impl<'c, 'ast> Visitor<'ast> for AstStaticTyper<'c> {
impl<'c, 'ast> Visitor<'ast> for AstPartiqlTyper<'c> {
fn enter_ast_node(&mut self, id: NodeId) -> Traverse {
self.id_stack.push(id);
Traverse::Continue
Expand Down Expand Up @@ -116,9 +116,9 @@ impl<'c, 'ast> Visitor<'ast> for AstStaticTyper<'c> {
Lit::NationalCharStringLit(_) => TypeKind::String,
Lit::BitStringLit(_) => todo!(),
Lit::HexStringLit(_) => todo!(),
Lit::StructLit(_) => TypeKind::Struct(StructType::unconstrained()),
Lit::ListLit(_) => TypeKind::Array(ArrayType::array()),
Lit::BagLit(_) => TypeKind::Bag(BagType::bag()),
Lit::StructLit(_) => TypeKind::Struct(StructType::new_any()),
Lit::ListLit(_) => TypeKind::Array(ArrayType::new_any()),
Lit::BagLit(_) => TypeKind::Bag(BagType::new_any()),
Lit::TypedLit(_, _) => todo!(),
};

Expand Down Expand Up @@ -161,7 +161,7 @@ impl<'c, 'ast> Visitor<'ast> for AstStaticTyper<'c> {
}
}

let ty = PartiqlType::new_struct(StructType::unconstrained());
let ty = PartiqlType::new_struct(StructType::new_any());
self.type_map.insert(id, ty.clone());

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

let id = *self.current_node();
let ty = PartiqlType::new_bag(BagType::bag());
let ty = PartiqlType::new_bag(BagType::new_any());

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

let id = *self.current_node();
let ty = PartiqlType::new_array(ArrayType::array());
let ty = PartiqlType::new_array(ArrayType::new_any());

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

#[test]
fn simple_test() {
Expand All @@ -244,22 +244,24 @@ mod tests {

#[test]
fn simple_err_test() {
assert!(type_statement("{'a': 1, a.b: 3}").is_err());
assert!(type_statement("{'a': 1, a.b: 3}", &PartiqlCatalog::default()).is_err());
}

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

fn type_statement(q: &str) -> Result<AstTypeMap<PartiqlType>, AstTransformationError> {
fn type_statement(
q: &str,
catalog: &dyn Catalog,
) -> Result<AstTypeMap<PartiqlType>, AstTransformationError> {
let parsed = partiql_parser::Parser::default()
.parse(q)
.expect("Expect successful parse");

let catalog = PartiqlCatalog::default();
let typer = AstStaticTyper::new(&catalog);
let typer = AstPartiqlTyper::new(catalog);
if let ast::Expr::Query(q) = parsed.ast.as_ref() {
typer.type_nodes(&q)
} else {
Expand Down
10 changes: 10 additions & 0 deletions partiql-catalog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ pub struct TypeEnvEntry<'a> {
ty: PartiqlType,
}

impl<'a> TypeEnvEntry<'a> {
pub fn new(name: &str, aliases: &[&'a str], ty: PartiqlType) -> Self {
TypeEnvEntry {
name: UniCase::from(name.to_string()),
aliases: aliases.to_vec(),
ty,
}
}
}

#[derive(Debug)]
pub struct TypeEntry {
id: ObjectId,
Expand Down
20 changes: 10 additions & 10 deletions partiql-conformance-tests/tests/test_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn parse_test_value_str(contents: &str) -> Value {
mod tests {
use super::parse_test_value_str;

use partiql_value::{partiql_bag, partiql_list, partiql_tuple, Value};
use partiql_value::{bag, list, tuple, Value};

#[track_caller]
fn parse(test: &str, expected: Value) {
Expand All @@ -64,7 +64,7 @@ mod tests {
s: 1
}
]";
let expected = Value::from(partiql_bag![partiql_tuple![
let expected = Value::from(bag![tuple![
("f", 1),
("d", Value::Real(2.0.into())),
("s", 1)
Expand All @@ -79,7 +79,7 @@ mod tests {
sensor: 1,
reading: 42
}",
Value::Tuple(Box::new(partiql_tuple![("sensor", 1), ("reading", 42)])),
Value::Tuple(Box::new(tuple![("sensor", 1), ("reading", 42)])),
);
}

Expand Down Expand Up @@ -110,17 +110,17 @@ mod tests {
}
]
}",
Value::Tuple(Box::new(partiql_tuple![
Value::Tuple(Box::new(tuple![
(
"sensors",
partiql_list![partiql_tuple![("sensor", 1)], partiql_tuple![("sensor", 2)]]
list![tuple![("sensor", 1)], tuple![("sensor", 2)]]
),
(
"logs",
partiql_list![
partiql_tuple![("sensor", 1), ("co", rust_decimal::Decimal::new(4, 1))],
partiql_tuple![("sensor", 1), ("co", rust_decimal::Decimal::new(2, 1))],
partiql_tuple![("sensor", 2), ("co", rust_decimal::Decimal::new(3, 1))]
list![
tuple![("sensor", 1), ("co", rust_decimal::Decimal::new(4, 1))],
tuple![("sensor", 1), ("co", rust_decimal::Decimal::new(2, 1))],
tuple![("sensor", 2), ("co", rust_decimal::Decimal::new(3, 1))]
]
)
])),
Expand All @@ -136,7 +136,7 @@ mod tests {
s: 1
}
]";
let expected = Value::from(partiql_list![partiql_tuple![
let expected = Value::from(list![tuple![
("f", 1),
("d", Value::Real(2.0.into())),
("s", 1)
Expand Down
30 changes: 15 additions & 15 deletions partiql-eval/benches/bench_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,36 @@ use partiql_eval::plan;
use partiql_logical as logical;
use partiql_logical::BindingsOp::{Project, ProjectAll};
use partiql_logical::{BinaryOp, BindingsOp, JoinKind, LogicalPlan, PathComponent, ValueExpr};
use partiql_value::{partiql_bag, partiql_list, partiql_tuple, BindingsName, Value};
use partiql_value::{bag, list, tuple, BindingsName, Value};

fn data() -> MapBindings<Value> {
let hr = partiql_tuple![(
let hr = tuple![(
"employeesNestScalars",
partiql_bag![
partiql_tuple![
bag![
tuple![
("id", 3),
("name", "Bob Smith"),
("title", Value::Null),
(
"projects",
partiql_list![
list![
"AWS Redshift Spectrum querying",
"AWS Redshift security",
"AWS Aurora security",
]
),
],
partiql_tuple![
tuple![
("id", 4),
("name", "Susan Smith"),
("title", "Dev Mgr"),
("projects", partiql_list![]),
("projects", list![]),
],
partiql_tuple![
tuple![
("id", 6),
("name", "Jane Smith"),
("title", "Software Eng 2"),
("projects", partiql_list!["AWS Redshift security"]),
("projects", list!["AWS Redshift security"]),
],
]
)];
Expand All @@ -50,14 +50,14 @@ fn data() -> MapBindings<Value> {
}

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

let orders = partiql_list![
partiql_tuple![("custId", 7), ("productId", 101)],
partiql_tuple![("custId", 7), ("productId", 523)],
let orders = list![
tuple![("custId", 7), ("productId", 101)],
tuple![("custId", 7), ("productId", 523)],
];

let mut bindings = MapBindings::default();
Expand Down
Loading

1 comment on commit bd7716d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PartiQL (rust) Benchmark

Benchmark suite Current: bd7716d Previous: fd60ab3 Ratio
parse-1 6001 ns/iter (± 18) 5866 ns/iter (± 10) 1.02
parse-15 56753 ns/iter (± 155) 56327 ns/iter (± 325) 1.01
parse-30 110650 ns/iter (± 354) 109739 ns/iter (± 653) 1.01
compile-1 4982 ns/iter (± 29) 4963 ns/iter (± 26) 1.00
compile-15 36153 ns/iter (± 33) 35386 ns/iter (± 31) 1.02
compile-30 73160 ns/iter (± 42) 71439 ns/iter (± 115) 1.02
plan-1 19469 ns/iter (± 20) 19707 ns/iter (± 35) 0.99
plan-15 357747 ns/iter (± 896) 360530 ns/iter (± 678) 0.99
plan-30 723337 ns/iter (± 1558) 726676 ns/iter (± 2432) 1.00
eval-1 26210844 ns/iter (± 1825101) 24591855 ns/iter (± 665164) 1.07
eval-15 121760759 ns/iter (± 1415036) 121105626 ns/iter (± 484672) 1.01
eval-30 229787046 ns/iter (± 1845399) 228895093 ns/iter (± 397360) 1.00
join 14369 ns/iter (± 27) 14304 ns/iter (± 73) 1.00
simple 6816 ns/iter (± 15) 6756 ns/iter (± 14) 1.01
simple-no 650 ns/iter (± 0) 691 ns/iter (± 1) 0.94
numbers 144 ns/iter (± 0) 144 ns/iter (± 0) 1
parse-simple 735 ns/iter (± 0) 728 ns/iter (± 0) 1.01
parse-ion 2660 ns/iter (± 5) 2613 ns/iter (± 11) 1.02
parse-group 8593 ns/iter (± 18) 8571 ns/iter (± 65) 1.00
parse-complex 21988 ns/iter (± 47) 21975 ns/iter (± 45) 1.00
parse-complex-fexpr 34789 ns/iter (± 83) 34815 ns/iter (± 82) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.