Skip to content

Commit

Permalink
refactor: remove any type (#5111)
Browse files Browse the repository at this point in the history
  • Loading branch information
aljazerzen authored Jan 27, 2025
1 parent 8104352 commit 5dbb506
Show file tree
Hide file tree
Showing 54 changed files with 2,293 additions and 3,457 deletions.
4 changes: 2 additions & 2 deletions lutra/lutra/src/pull_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ fn convert_arrow_schema_to_table_def(table_name: String, schema: SchemaRef) -> R
kind: prqlc::pr::VarDefKind::Let,
name: table_name,
value: None,
ty: Some(Ty::new(TyKind::Array(Box::new(Ty::new(TyKind::Tuple(
fields,
ty: Some(Ty::new(TyKind::Array(Some(Box::new(Ty::new(
TyKind::Tuple(fields),
)))))),
};

Expand Down
4 changes: 2 additions & 2 deletions prqlc/bindings/prqlc-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,15 @@ mod test {
fn debug_prql_lineage() {
assert_snapshot!(
debug::prql_lineage(r#"from a | select { beta, gamma }"#).unwrap(),
@r#"{"frames":[["1:9-31",{"columns":[{"Single":{"name":["a","beta"],"target_id":120,"target_name":null}},{"Single":{"name":["a","gamma"],"target_id":121,"target_name":null}}],"inputs":[{"id":118,"name":"a","table":["default_db","a"]}]}]],"nodes":[{"id":118,"kind":"Ident","span":"1:0-6","ident":{"Ident":["default_db","a"]},"parent":123},{"id":120,"kind":"Ident","span":"1:18-22","ident":{"Ident":["this","a","beta"]},"targets":[118],"parent":122},{"id":121,"kind":"Ident","span":"1:24-29","ident":{"Ident":["this","a","gamma"]},"targets":[118],"parent":122},{"id":122,"kind":"Tuple","span":"1:16-31","children":[120,121],"parent":123},{"id":123,"kind":"TransformCall: Select","span":"1:9-31","children":[118,122]}],"ast":{"name":"Project","stmts":[{"VarDef":{"kind":"Main","name":"main","value":{"Pipeline":{"exprs":[{"FuncCall":{"name":{"Ident":"from","span":"1:0-4"},"args":[{"Ident":"a","span":"1:5-6"}]},"span":"1:0-6"},{"FuncCall":{"name":{"Ident":"select","span":"1:9-15"},"args":[{"Tuple":[{"Ident":"beta","span":"1:18-22"},{"Ident":"gamma","span":"1:24-29"}],"span":"1:16-31"}]},"span":"1:9-31"}]},"span":"1:0-31"}},"span":"1:0-31"}]}}"#
@r#"{"frames":[["1:9-31",{"columns":[{"Single":{"name":["a","beta"],"target_id":117,"target_name":null}},{"Single":{"name":["a","gamma"],"target_id":118,"target_name":null}}],"inputs":[{"id":115,"name":"a","table":["default_db","a"]}]}]],"nodes":[{"id":115,"kind":"Ident","span":"1:0-6","ident":{"Ident":["default_db","a"]},"parent":120},{"id":117,"kind":"Ident","span":"1:18-22","ident":{"Ident":["this","a","beta"]},"targets":[115],"parent":119},{"id":118,"kind":"Ident","span":"1:24-29","ident":{"Ident":["this","a","gamma"]},"targets":[115],"parent":119},{"id":119,"kind":"Tuple","span":"1:16-31","children":[117,118],"parent":120},{"id":120,"kind":"TransformCall: Select","span":"1:9-31","children":[115,119]}],"ast":{"name":"Project","stmts":[{"VarDef":{"kind":"Main","name":"main","value":{"Pipeline":{"exprs":[{"FuncCall":{"name":{"Ident":"from","span":"1:0-4"},"args":[{"Ident":"a","span":"1:5-6"}]},"span":"1:0-6"},{"FuncCall":{"name":{"Ident":"select","span":"1:9-15"},"args":[{"Tuple":[{"Ident":"beta","span":"1:18-22"},{"Ident":"gamma","span":"1:24-29"}],"span":"1:16-31"}]},"span":"1:9-31"}]},"span":"1:0-31"}},"span":"1:0-31"}]}}"#
);
}

#[test]
fn debug_pl_to_lineage() {
assert_snapshot!(
prql_to_pl(r#"from a | select { beta, gamma }"#).and_then(|x| debug::pl_to_lineage(&x)).unwrap(),
@r#"{"frames":[["1:9-31",{"columns":[{"Single":{"name":["a","beta"],"target_id":120,"target_name":null}},{"Single":{"name":["a","gamma"],"target_id":121,"target_name":null}}],"inputs":[{"id":118,"name":"a","table":["default_db","a"]}]}]],"nodes":[{"id":118,"kind":"Ident","span":"1:0-6","ident":{"Ident":["default_db","a"]},"parent":123},{"id":120,"kind":"Ident","span":"1:18-22","ident":{"Ident":["this","a","beta"]},"targets":[118],"parent":122},{"id":121,"kind":"Ident","span":"1:24-29","ident":{"Ident":["this","a","gamma"]},"targets":[118],"parent":122},{"id":122,"kind":"Tuple","span":"1:16-31","children":[120,121],"parent":123},{"id":123,"kind":"TransformCall: Select","span":"1:9-31","children":[118,122]}],"ast":{"name":"Project","stmts":[{"VarDef":{"kind":"Main","name":"main","value":{"Pipeline":{"exprs":[{"FuncCall":{"name":{"Ident":"from","span":"1:0-4"},"args":[{"Ident":"a","span":"1:5-6"}]},"span":"1:0-6"},{"FuncCall":{"name":{"Ident":"select","span":"1:9-15"},"args":[{"Tuple":[{"Ident":"beta","span":"1:18-22"},{"Ident":"gamma","span":"1:24-29"}],"span":"1:16-31"}]},"span":"1:9-31"}]},"span":"1:0-31"}},"span":"1:0-31"}]}}"#
@r#"{"frames":[["1:9-31",{"columns":[{"Single":{"name":["a","beta"],"target_id":117,"target_name":null}},{"Single":{"name":["a","gamma"],"target_id":118,"target_name":null}}],"inputs":[{"id":115,"name":"a","table":["default_db","a"]}]}]],"nodes":[{"id":115,"kind":"Ident","span":"1:0-6","ident":{"Ident":["default_db","a"]},"parent":120},{"id":117,"kind":"Ident","span":"1:18-22","ident":{"Ident":["this","a","beta"]},"targets":[115],"parent":119},{"id":118,"kind":"Ident","span":"1:24-29","ident":{"Ident":["this","a","gamma"]},"targets":[115],"parent":119},{"id":119,"kind":"Tuple","span":"1:16-31","children":[117,118],"parent":120},{"id":120,"kind":"TransformCall: Select","span":"1:9-31","children":[115,119]}],"ast":{"name":"Project","stmts":[{"VarDef":{"kind":"Main","name":"main","value":{"Pipeline":{"exprs":[{"FuncCall":{"name":{"Ident":"from","span":"1:0-4"},"args":[{"Ident":"a","span":"1:5-6"}]},"span":"1:0-6"},{"FuncCall":{"name":{"Ident":"select","span":"1:9-15"},"args":[{"Tuple":[{"Ident":"beta","span":"1:18-22"},{"Ident":"gamma","span":"1:24-29"}],"span":"1:16-31"}]},"span":"1:9-31"}]},"span":"1:0-31"}},"span":"1:0-31"}]}}"#
);
}
}
1 change: 0 additions & 1 deletion prqlc/prqlc-parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ pub(crate) fn prepare_stream<'a>(
fn ident_part() -> impl Parser<TokenKind, String, Error = PError> + Clone {
select! {
TokenKind::Ident(ident) => ident,
TokenKind::Keyword(ident) if &ident == "module" => ident,
}
.map_err(|e: PError| {
PError::expected_input_found(
Expand Down
27 changes: 6 additions & 21 deletions prqlc/prqlc-parser/src/parser/pr/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,15 @@ pub enum TyKind {
/// Type that contains only a one value
Singleton(Literal),

/// Union of sets (sum)
Union(Vec<(Option<String>, Ty)>),

/// Type of tuples (product)
Tuple(Vec<TyTupleField>),

/// Type of arrays
Array(Box<Ty>),
Array(Option<Box<Ty>>),

/// Type of functions with defined params and return types.
Function(Option<TyFunc>),

/// Type of every possible value. Super type of all other types.
/// The breaker of chains. Mother of types.
Any,

/// Type that is the largest subtype of `base` while not a subtype of `exclude`.
Difference { base: Box<Ty>, exclude: Box<Ty> },

Expand Down Expand Up @@ -119,32 +112,24 @@ impl Ty {

pub fn relation(tuple_fields: Vec<TyTupleField>) -> Self {
let tuple = Ty::new(TyKind::Tuple(tuple_fields));
Ty::new(TyKind::Array(Box::new(tuple)))
}

pub fn never() -> Self {
Ty::new(TyKind::Union(Vec::new()))
}

pub fn is_never(&self) -> bool {
self.kind.as_union().map_or(false, |x| x.is_empty())
Ty::new(TyKind::Array(Some(Box::new(tuple))))
}

pub fn as_relation(&self) -> Option<&Vec<TyTupleField>> {
self.kind.as_array()?.kind.as_tuple()
self.kind.as_array()?.as_ref()?.kind.as_tuple()
}

pub fn as_relation_mut(&mut self) -> Option<&mut Vec<TyTupleField>> {
self.kind.as_array_mut()?.kind.as_tuple_mut()
self.kind.as_array_mut()?.as_mut()?.kind.as_tuple_mut()
}

pub fn into_relation(self) -> Option<Vec<TyTupleField>> {
self.kind.into_array().ok()?.kind.into_tuple().ok()
self.kind.into_array().ok()??.kind.into_tuple().ok()
}

pub fn is_relation(&self) -> bool {
match &self.kind {
TyKind::Array(elem) => {
TyKind::Array(Some(elem)) => {
matches!(elem.kind, TyKind::Tuple(_))
}
_ => false,
Expand Down
59 changes: 9 additions & 50 deletions prqlc/prqlc-parser/src/parser/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub(crate) fn type_expr() -> impl Parser<TokenKind, Ty, Error = PError> + Clone
TokenKind::Ident(i) if i == "date"=> TyKind::Primitive(PrimitiveSet::Date),
TokenKind::Ident(i) if i == "time"=> TyKind::Primitive(PrimitiveSet::Time),
TokenKind::Ident(i) if i == "timestamp"=> TyKind::Primitive(PrimitiveSet::Timestamp),
TokenKind::Ident(i) if i == "anytype"=> TyKind::Any,
};

let ident = ident().map(TyKind::Ident);
Expand All @@ -40,14 +39,16 @@ pub(crate) fn type_expr() -> impl Parser<TokenKind, Ty, Error = PError> + Clone
.map(TyKind::Function);

let tuple = sequence(choice((
select! { TokenKind::Range { bind_right: false, bind_left: _ } => () }
.to(TyTupleField::Wildcard(None)),
select! { TokenKind::Range { bind_right: true, bind_left: _ } => () }
.ignore_then(nested_type_expr.clone())
.map(|ty| TyTupleField::Wildcard(Some(ty))),
.ignore_then(nested_type_expr.clone().or_not())
.map(TyTupleField::Wildcard),
ident_part()
.then_ignore(ctrl('='))
.or_not()
.then(nested_type_expr.clone())
.map(|(name, ty)| TyTupleField::Single(name, Some(ty))),
.then(ctrl('*').to(None).or(nested_type_expr.clone().map(Some)))
.map(|(name, ty)| TyTupleField::Single(name, ty)),
)))
.delimited_by(ctrl('{'), ctrl('}'))
.recover_with(nested_delimiters(
Expand Down Expand Up @@ -76,35 +77,9 @@ pub(crate) fn type_expr() -> impl Parser<TokenKind, Ty, Error = PError> + Clone
.map(TyKind::Tuple)
.labelled("tuple");

let enum_ = keyword("enum")
.ignore_then(
sequence(
ident_part()
.then(ctrl('=').ignore_then(nested_type_expr.clone()).or_not())
.map(|(name, ty)| {
(
Some(name),
ty.unwrap_or_else(|| Ty::new(TyKind::Tuple(vec![]))),
)
}),
)
.delimited_by(ctrl('{'), ctrl('}'))
.recover_with(nested_delimiters(
TokenKind::Control('{'),
TokenKind::Control('}'),
[
(TokenKind::Control('{'), TokenKind::Control('}')),
(TokenKind::Control('('), TokenKind::Control(')')),
(TokenKind::Control('['), TokenKind::Control(']')),
],
|_| vec![],
)),
)
.map(TyKind::Union)
.labelled("union");

let array = nested_type_expr
.map(Box::new)
.or_not()
.padded_by(new_line().repeated())
.delimited_by(ctrl('['), ctrl(']'))
.recover_with(nested_delimiters(
Expand All @@ -115,14 +90,12 @@ pub(crate) fn type_expr() -> impl Parser<TokenKind, Ty, Error = PError> + Clone
(TokenKind::Control('('), TokenKind::Control(')')),
(TokenKind::Control('['), TokenKind::Control(']')),
],
|_| Box::new(Ty::new(TyKind::Tuple(vec![]))),
|_| None,
))
.map(TyKind::Array)
.labelled("array");

let term = choice((basic, ident, func, tuple, array, enum_))
.map_with_span(TyKind::into_ty)
.boxed();
choice((basic, ident, func, tuple, array)).map_with_span(TyKind::into_ty)

// exclude
// term.clone()
Expand All @@ -142,20 +115,6 @@ pub(crate) fn type_expr() -> impl Parser<TokenKind, Ty, Error = PError> + Clone
// };
// into_ty(kind, span)
// });

// union
term.clone()
.then(just(TokenKind::Or).ignore_then(term).repeated())
.map_with_span(|(first, following), span| {
if following.is_empty() {
first
} else {
let mut all = Vec::with_capacity(following.len() + 1);
all.push((None, first));
all.extend(following.into_iter().map(|x| (None, x)));
TyKind::Union(all).into_ty(span)
}
})
})
.labelled("type expression")
}
14 changes: 0 additions & 14 deletions prqlc/prqlc/src/cli/docs_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ pub fn generate_html_docs(stmts: Vec<Stmt>) -> String {
if let Some(return_ty) = &func.return_ty {
docs.push_str(" <h4 class=\"h6\">Returns</h4>\n");
match &return_ty.kind {
TyKind::Any => docs.push_str(" <p>Any</p>\n"),
TyKind::Ident(ident) => {
docs.push_str(&format!(" <p><code>{}</code></p>\n", ident.name));
}
Expand All @@ -170,13 +169,6 @@ pub fn generate_html_docs(stmts: Vec<Stmt>) -> String {
TyKind::Singleton(literal) => {
docs.push_str(&format!(" <p><code>{literal}</code></p>\n"));
}
TyKind::Union(vec) => {
docs.push_str(" <ul class=\"list-unstyled\">\n");
for (_, ty) in vec {
docs.push_str(&format!(" <li>{:?}</li>\n", ty.kind));
}
docs.push_str(" </ul>\n");
}
_ => docs.push_str(" <p class=\"text-danger\">Not implemented</p>\n"),
}
}
Expand Down Expand Up @@ -319,7 +311,6 @@ Generated with [prqlc](https://prql-lang.org/) {}.
if let Some(return_ty) = &func.return_ty {
docs.push_str("#### Returns\n");
match &return_ty.kind {
TyKind::Any => docs.push_str("Any\n"),
TyKind::Ident(ident) => {
docs.push_str(&format!("`{}`\n", ident.name));
}
Expand All @@ -329,11 +320,6 @@ Generated with [prqlc](https://prql-lang.org/) {}.
TyKind::Singleton(literal) => {
docs.push_str(&format!("`{literal}`\n"));
}
TyKind::Union(vec) => {
for (_, ty) in vec {
docs.push_str(&format!("* {:?}\n", ty.kind));
}
}
_ => docs.push_str("Not implemented\n"),
}
}
Expand Down
40 changes: 20 additions & 20 deletions prqlc/prqlc/src/cli/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ fn compare_directories(dir1: &Path, dir2: &Path) {
fn debug() {
assert_cmd_snapshot!(prqlc_command()
.args(["debug", "lineage"])
.pass_stdin("from tracks | select {artist, album}"), @r###"
.pass_stdin("from tracks | select {artist, album}"), @r#"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -405,61 +405,61 @@ fn debug() {
name:
- tracks
- artist
target_id: 120
target_id: 117
target_name: null
- !Single
name:
- tracks
- album
target_id: 121
target_id: 118
target_name: null
inputs:
- id: 118
- id: 115
name: tracks
table:
- default_db
- tracks
nodes:
- id: 118
- id: 115
kind: Ident
span: 1:0-11
ident: !Ident
- default_db
- tracks
parent: 123
- id: 120
parent: 120
- id: 117
kind: Ident
span: 1:22-28
ident: !Ident
- this
- tracks
- artist
targets:
- 118
parent: 122
- id: 121
- 115
parent: 119
- id: 118
kind: Ident
span: 1:30-35
ident: !Ident
- this
- tracks
- album
targets:
- 118
parent: 122
- id: 122
- 115
parent: 119
- id: 119
kind: Tuple
span: 1:21-36
children:
- 120
- 121
parent: 123
- id: 123
- 117
- 118
parent: 120
- id: 120
kind: 'TransformCall: Select'
span: 1:14-36
children:
- 118
- 122
- 115
- 119
ast:
name: Project
stmts:
Expand Down Expand Up @@ -493,7 +493,7 @@ fn debug() {
span: 1:0-36
----- stderr -----
"###);
"#);

// Don't test the output of this, since on one min-versions check it had
// different results, and didn't repro on Mac. It having different results
Expand Down
Loading

0 comments on commit 5dbb506

Please sign in to comment.