Skip to content

Commit 1fc44de

Browse files
authored
Refactors to make working with types a bit more ergonomic (#478)
1 parent c91c5a4 commit 1fc44de

File tree

4 files changed

+51
-15
lines changed

4 files changed

+51
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- *BREAKING:* partiql-ast: changed modeling of `PathStep` to split `PathExpr` to `PathIndex` (e.g., `[2]`) and `PathProject` (e.g., `.a`)
1313
- *BREAKING:* partiql-ast: changed modeling of `PathStep` to rename `PathWildcard` to `PathForEach` (for `[*]`)
1414
- *BREAKING:* partiql-types: changed type ordering to match specification order
15+
- *BREAKING:* partiql-types: changed some interfaces to reduce clones and be more ergonomic
1516

1617
### Added
1718
- partiql-ast: Pretty-printing of AST via `ToPretty` trait

extension/partiql-extension-ddl/src/ddl.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ impl PartiqlBasicDdlEncoder {
122122
Static::Float32 => out.push_str("REAL"),
123123
Static::Float64 => out.push_str("DOUBLE"),
124124
Static::String => out.push_str("VARCHAR"),
125-
Static::Struct(s) => out.push_str(&self.write_struct(&s)?),
126-
Static::Bag(b) => out.push_str(&self.write_bag(&b)?),
127-
Static::Array(a) => out.push_str(&self.write_array(&a)?),
125+
Static::Struct(s) => out.push_str(&self.write_struct(s)?),
126+
Static::Bag(b) => out.push_str(&self.write_bag(b)?),
127+
Static::Array(a) => out.push_str(&self.write_array(a)?),
128128
// non-exhaustive catch-all
129129
_ => todo!("handle type for {}", ty),
130130
}
@@ -147,8 +147,7 @@ impl PartiqlBasicDdlEncoder {
147147
fn write_struct(&self, strct: &StructType) -> ShapeDdlEncodeResult<String> {
148148
let mut struct_out = String::from("STRUCT<");
149149

150-
let fields = strct.fields();
151-
let mut fields = fields.iter().peekable();
150+
let mut fields = strct.fields().peekable();
152151
while let Some(field) = fields.next() {
153152
struct_out.push_str(&format!("\"{}\": ", field.name()));
154153
struct_out.push_str(&self.write_shape(field.ty())?);
@@ -192,8 +191,7 @@ impl PartiqlDdlEncoder for PartiqlBasicDdlEncoder {
192191

193192
if let Static::Bag(bag) = ty.ty() {
194193
let s = bag.element_type().expect_struct()?;
195-
let fields = s.fields();
196-
let mut fields = fields.iter().peekable();
194+
let mut fields = s.fields().peekable();
197195
while let Some(field) = fields.next() {
198196
output.push_str(&format!("\"{}\" ", field.name()));
199197

partiql-logical-planner/src/typer.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,6 @@ fn string_to_sym(name: &str) -> SymbolPrimitive {
580580

581581
fn to_bindings(s: &StructType) -> Vec<(SymbolPrimitive, PartiqlShape)> {
582582
s.fields()
583-
.into_iter()
584583
.map(|field| {
585584
let sym = SymbolPrimitive {
586585
value: field.name().to_string(),
@@ -883,7 +882,7 @@ mod tests {
883882
match &actual.ty() {
884883
Static::Bag(b) => {
885884
if let Ok(s) = b.element_type().expect_struct() {
886-
let fields = s.fields();
885+
let fields: IndexSet<_> = s.fields().collect();
887886

888887
let f: Vec<_> = expected_fields
889888
.iter()

partiql-types/src/lib.rs

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,23 +196,37 @@ pub enum Static {
196196
// TODO Add BitString, ByteString, Blob, Clob, and Graph types
197197
}
198198

199+
impl Static {
200+
pub fn is_scalar(&self) -> bool {
201+
!matches!(self, Static::Struct(_) | Static::Bag(_) | Static::Array(_))
202+
}
203+
204+
pub fn is_sequence(&self) -> bool {
205+
matches!(self, Static::Bag(_) | Static::Array(_))
206+
}
207+
208+
pub fn is_struct(&self) -> bool {
209+
matches!(self, Static::Struct(_))
210+
}
211+
}
212+
199213
impl StaticType {
200214
#[must_use]
201-
pub fn new(&self, ty: Static) -> StaticType {
215+
pub fn new(ty: Static) -> StaticType {
202216
StaticType { ty, nullable: true }
203217
}
204218

205219
#[must_use]
206-
pub fn new_non_nullable(&self, ty: Static) -> StaticType {
220+
pub fn new_non_nullable(ty: Static) -> StaticType {
207221
StaticType {
208222
ty,
209223
nullable: false,
210224
}
211225
}
212226

213227
#[must_use]
214-
pub fn ty(&self) -> Static {
215-
self.ty.clone()
228+
pub fn ty(&self) -> &Static {
229+
&self.ty
216230
}
217231

218232
#[must_use]
@@ -224,6 +238,18 @@ impl StaticType {
224238
pub fn is_not_nullable(&self) -> bool {
225239
!self.nullable
226240
}
241+
242+
pub fn is_scalar(&self) -> bool {
243+
self.ty.is_scalar()
244+
}
245+
246+
pub fn is_sequence(&self) -> bool {
247+
self.ty.is_sequence()
248+
}
249+
250+
pub fn is_struct(&self) -> bool {
251+
self.ty.is_struct()
252+
}
227253
}
228254

229255
impl Display for StaticType {
@@ -565,8 +591,7 @@ impl StructType {
565591
}
566592
}
567593

568-
#[must_use]
569-
pub fn fields(&self) -> IndexSet<StructField> {
594+
pub fn fields_set(&self) -> IndexSet<StructField> {
570595
self.constraints
571596
.iter()
572597
.flat_map(|c| {
@@ -579,6 +604,19 @@ impl StructType {
579604
.collect()
580605
}
581606

607+
pub fn fields(&self) -> impl Iterator<Item = &StructField> {
608+
self.constraints
609+
.iter()
610+
.filter_map(|c| {
611+
if let StructConstraint::Fields(fields) = c {
612+
Some(fields)
613+
} else {
614+
None
615+
}
616+
})
617+
.flat_map(|f| f.iter())
618+
}
619+
582620
#[must_use]
583621
pub fn is_partial(&self) -> bool {
584622
!self.is_closed()

0 commit comments

Comments
 (0)