Skip to content

Commit a3f64f5

Browse files
authored
Rename StaticType and add TypeEnv to the Catalog (#396)
- Adds TypeEnv to the Catalog - Renames StaticType to refrain from conveying an inaccurate message that this type is only used for Static typing. with PartiQL b/c seemingly, we need to do a combination of Static And Dynamic Typing.
1 parent dcb3c0b commit a3f64f5

File tree

5 files changed

+120
-81
lines changed

5 files changed

+120
-81
lines changed

partiql-ast-passes/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
77
pub mod error;
88
pub mod name_resolver;
9-
pub mod static_typer;
9+
pub mod partiql_typer;

partiql-ast-passes/src/static_typer.rs renamed to partiql-ast-passes/src/partiql_typer.rs

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ use partiql_ast::ast::{
44
};
55
use partiql_ast::visit::{Traverse, Visit, Visitor};
66
use partiql_catalog::Catalog;
7-
use partiql_types::{ArrayType, BagType, StaticType, StaticTypeKind, StructType};
7+
use partiql_types::{ArrayType, BagType, PartiqlType, StructType, TypeKind};
88

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

@@ -30,7 +30,7 @@ impl<'c> AstStaticTyper<'c> {
3030
pub fn type_nodes(
3131
mut self,
3232
query: &AstNode<Query>,
33-
) -> Result<AstTypeMap<StaticType>, AstTransformationError> {
33+
) -> Result<AstTypeMap<PartiqlType>, AstTransformationError> {
3434
query.visit(&mut self);
3535
if self.errors.is_empty() {
3636
Ok(self.type_map)
@@ -99,30 +99,30 @@ impl<'c, 'ast> Visitor<'ast> for AstStaticTyper<'c> {
9999
// Currently we're assuming no-schema, hence typing to arbitrary sized scalars.
100100
// TODO type to the corresponding scalar with the introduction of schema
101101
let kind = match _lit {
102-
Lit::Null => StaticTypeKind::Null,
103-
Lit::Missing => StaticTypeKind::Missing,
104-
Lit::Int8Lit(_) => StaticTypeKind::Int,
105-
Lit::Int16Lit(_) => StaticTypeKind::Int,
106-
Lit::Int32Lit(_) => StaticTypeKind::Int,
107-
Lit::Int64Lit(_) => StaticTypeKind::Int,
108-
Lit::DecimalLit(_) => StaticTypeKind::Decimal,
109-
Lit::NumericLit(_) => StaticTypeKind::Decimal,
110-
Lit::RealLit(_) => StaticTypeKind::Float64,
111-
Lit::FloatLit(_) => StaticTypeKind::Float64,
112-
Lit::DoubleLit(_) => StaticTypeKind::Float64,
113-
Lit::BoolLit(_) => StaticTypeKind::Bool,
102+
Lit::Null => TypeKind::Null,
103+
Lit::Missing => TypeKind::Missing,
104+
Lit::Int8Lit(_) => TypeKind::Int,
105+
Lit::Int16Lit(_) => TypeKind::Int,
106+
Lit::Int32Lit(_) => TypeKind::Int,
107+
Lit::Int64Lit(_) => TypeKind::Int,
108+
Lit::DecimalLit(_) => TypeKind::Decimal,
109+
Lit::NumericLit(_) => TypeKind::Decimal,
110+
Lit::RealLit(_) => TypeKind::Float64,
111+
Lit::FloatLit(_) => TypeKind::Float64,
112+
Lit::DoubleLit(_) => TypeKind::Float64,
113+
Lit::BoolLit(_) => TypeKind::Bool,
114114
Lit::IonStringLit(_) => todo!(),
115-
Lit::CharStringLit(_) => StaticTypeKind::String,
116-
Lit::NationalCharStringLit(_) => StaticTypeKind::String,
115+
Lit::CharStringLit(_) => TypeKind::String,
116+
Lit::NationalCharStringLit(_) => TypeKind::String,
117117
Lit::BitStringLit(_) => todo!(),
118118
Lit::HexStringLit(_) => todo!(),
119-
Lit::StructLit(_) => StaticTypeKind::Struct(StructType::unconstrained()),
120-
Lit::ListLit(_) => StaticTypeKind::Array(ArrayType::array()),
121-
Lit::BagLit(_) => StaticTypeKind::Bag(BagType::bag()),
119+
Lit::StructLit(_) => TypeKind::Struct(StructType::unconstrained()),
120+
Lit::ListLit(_) => TypeKind::Array(ArrayType::array()),
121+
Lit::BagLit(_) => TypeKind::Bag(BagType::bag()),
122122
Lit::TypedLit(_, _) => todo!(),
123123
};
124124

125-
let ty = StaticType::new(kind);
125+
let ty = PartiqlType::new(kind);
126126
let id = *self.current_node();
127127
if let Some(c) = self.container_stack.last_mut() {
128128
c.push(ty.clone())
@@ -161,7 +161,7 @@ impl<'c, 'ast> Visitor<'ast> for AstStaticTyper<'c> {
161161
}
162162
}
163163

164-
let ty = StaticType::new_struct(StructType::unconstrained());
164+
let ty = PartiqlType::new_struct(StructType::unconstrained());
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 = StaticType::new_bag(BagType::bag());
187+
let ty = PartiqlType::new_bag(BagType::bag());
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 = StaticType::new_array(ArrayType::array());
209+
let ty = PartiqlType::new_array(ArrayType::array());
210210

211211
self.type_map.insert(id, ty.clone());
212212
if let Some(s) = self.container_stack.last_mut() {
@@ -222,29 +222,23 @@ mod tests {
222222
use assert_matches::assert_matches;
223223
use partiql_ast::ast;
224224
use partiql_catalog::PartiqlCatalog;
225-
use partiql_types::{StaticType, StaticTypeKind};
225+
use partiql_types::{PartiqlType, TypeKind};
226226

227227
#[test]
228228
fn simple_test() {
229-
assert_matches!(run_literal_test("NULL"), StaticTypeKind::Null);
230-
assert_matches!(run_literal_test("MISSING"), StaticTypeKind::Missing);
231-
assert_matches!(run_literal_test("Missing"), StaticTypeKind::Missing);
232-
assert_matches!(run_literal_test("true"), StaticTypeKind::Bool);
233-
assert_matches!(run_literal_test("false"), StaticTypeKind::Bool);
234-
assert_matches!(run_literal_test("1"), StaticTypeKind::Int);
235-
assert_matches!(run_literal_test("1.5"), StaticTypeKind::Decimal);
236-
assert_matches!(run_literal_test("'hello world!'"), StaticTypeKind::String);
237-
assert_matches!(
238-
run_literal_test("[1, 2 , {'a': 2}]"),
239-
StaticTypeKind::Array(_)
240-
);
241-
assert_matches!(
242-
run_literal_test("<<'1', {'a': 11}>>"),
243-
StaticTypeKind::Bag(_)
244-
);
229+
assert_matches!(run_literal_test("NULL"), TypeKind::Null);
230+
assert_matches!(run_literal_test("MISSING"), TypeKind::Missing);
231+
assert_matches!(run_literal_test("Missing"), TypeKind::Missing);
232+
assert_matches!(run_literal_test("true"), TypeKind::Bool);
233+
assert_matches!(run_literal_test("false"), TypeKind::Bool);
234+
assert_matches!(run_literal_test("1"), TypeKind::Int);
235+
assert_matches!(run_literal_test("1.5"), TypeKind::Decimal);
236+
assert_matches!(run_literal_test("'hello world!'"), TypeKind::String);
237+
assert_matches!(run_literal_test("[1, 2 , {'a': 2}]"), TypeKind::Array(_));
238+
assert_matches!(run_literal_test("<<'1', {'a': 11}>>"), TypeKind::Bag(_));
245239
assert_matches!(
246240
run_literal_test("{'a': 1, 'b': 3, 'c': [1, 2]}"),
247-
StaticTypeKind::Struct(_)
241+
TypeKind::Struct(_)
248242
);
249243
}
250244

@@ -253,13 +247,13 @@ mod tests {
253247
assert!(type_statement("{'a': 1, a.b: 3}").is_err());
254248
}
255249

256-
fn run_literal_test(q: &str) -> StaticTypeKind {
250+
fn run_literal_test(q: &str) -> TypeKind {
257251
let out = type_statement(q).expect("type map");
258-
let values: Vec<&StaticType> = out.values().collect();
252+
let values: Vec<&PartiqlType> = out.values().collect();
259253
values.last().unwrap().kind().clone()
260254
}
261255

262-
fn type_statement(q: &str) -> Result<AstTypeMap<StaticType>, AstTransformationError> {
256+
fn type_statement(q: &str) -> Result<AstTypeMap<PartiqlType>, AstTransformationError> {
263257
let parsed = partiql_parser::Parser::default()
264258
.parse(q)
265259
.expect("Expect successful parse");

partiql-catalog/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ bench = false
2424
partiql-value = { path = "../partiql-value", version = "0.5.*" }
2525
partiql-parser = { path = "../partiql-parser", version = "0.5.*" }
2626
partiql-logical = { path = "../partiql-logical", version = "0.5.*" }
27+
partiql-types = { path = "../partiql-types", version = "0.5.*" }
2728

2829
thiserror = "1.0"
2930
ordered-float = "3.*"

partiql-catalog/src/lib.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::call_defs::CallDef;
22

3+
use partiql_types::PartiqlType;
34
use partiql_value::Value;
45
use std::borrow::Cow;
56

@@ -103,7 +104,24 @@ pub enum CatalogErrorKind {
103104
pub trait Catalog: Debug {
104105
fn add_table_function(&mut self, info: TableFunction) -> Result<ObjectId, CatalogError>;
105106

107+
fn add_type_entry(&mut self, entry: TypeEnvEntry) -> Result<ObjectId, CatalogError>;
108+
106109
fn get_function(&self, name: &str) -> Option<FunctionEntry>;
110+
111+
fn resolve_type(&self, name: &str) -> Option<TypeEntry>;
112+
}
113+
114+
#[derive(Debug)]
115+
pub struct TypeEnvEntry<'a> {
116+
name: UniCase<String>,
117+
aliases: Vec<&'a str>,
118+
ty: PartiqlType,
119+
}
120+
121+
#[derive(Debug)]
122+
pub struct TypeEntry {
123+
id: ObjectId,
124+
ty: PartiqlType,
107125
}
108126

109127
#[derive(Debug)]
@@ -140,13 +158,15 @@ impl<'a> FunctionEntry<'a> {
140158
#[derive(Debug)]
141159
pub struct PartiqlCatalog {
142160
functions: CatalogEntrySet<FunctionEntryFunction>,
161+
types: CatalogEntrySet<PartiqlType>,
143162
id: CatalogId,
144163
}
145164

146165
impl Default for PartiqlCatalog {
147166
fn default() -> Self {
148167
PartiqlCatalog {
149168
functions: Default::default(),
169+
types: Default::default(),
150170
id: CatalogId(1),
151171
}
152172
}
@@ -173,6 +193,20 @@ impl Catalog for PartiqlCatalog {
173193
}
174194
}
175195

196+
fn add_type_entry(&mut self, entry: TypeEnvEntry) -> Result<ObjectId, CatalogError> {
197+
let id = self
198+
.types
199+
.add(entry.name.as_ref(), entry.aliases.as_slice(), entry.ty);
200+
201+
match id {
202+
Ok(id) => Ok(ObjectId {
203+
catalog_id: self.id,
204+
entry_id: id,
205+
}),
206+
Err(e) => Err(e),
207+
}
208+
}
209+
176210
fn get_function(&self, name: &str) -> Option<FunctionEntry> {
177211
self.functions
178212
.find_by_name(name)
@@ -184,6 +218,16 @@ impl Catalog for PartiqlCatalog {
184218
function: entry,
185219
})
186220
}
221+
222+
fn resolve_type(&self, name: &str) -> Option<TypeEntry> {
223+
self.types.find_by_name(name).map(|(eid, entry)| TypeEntry {
224+
id: ObjectId {
225+
catalog_id: self.id,
226+
entry_id: eid,
227+
},
228+
ty: entry.clone(),
229+
})
230+
}
187231
}
188232

189233
#[derive(Debug)]

0 commit comments

Comments
 (0)