Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: pass parent to and finally fix define aggregate
  • Loading branch information
psteinroe committed Dec 17, 2023
commit 07539124f5402b9600ed6cf147bd0f75bdb4ff2f
50 changes: 29 additions & 21 deletions crates/codegen/src/get_node_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub fn get_node_properties_mod(proto_file: &ProtoFile) -> proc_macro2::TokenStre
}
}

pub fn get_node_properties(node: &NodeEnum) -> Vec<TokenProperty> {
pub fn get_node_properties(node: &NodeEnum, parent: Option<&NodeEnum>) -> Vec<TokenProperty> {
let mut tokens: Vec<TokenProperty> = Vec::new();

match node {
Expand Down Expand Up @@ -481,6 +481,32 @@ fn custom_handlers(node: &Node) -> TokenStream {
tokens.push(TokenProperty::from(Token::As));
}
},
"List" => quote! {
if parent.is_some() {
// if parent is `DefineStmt`, we need to check whether an ORDER BY needs to be added
if let NodeEnum::DefineStmt(define_stmt) = parent.unwrap() {
// there *seems* to be an integer node in the last position of the DefineStmt args that
// defines whether the list contains an order by statement
let integer = define_stmt.args.last()
.and_then(|node| node.node.as_ref())
.and_then(|node| if let NodeEnum::Integer(n) = node { Some(n.ival) } else { None });
if integer.is_none() {
panic!("DefineStmt of type ObjectAggregate has no integer node in last position of args");
}
// if the integer is 1, then there is an order by statement
// we add it to the `List` node because that seems to make most sense based off the grammar definition
// ref: https://github.com/postgres/postgres/blob/REL_15_STABLE/src/backend/parser/gram.y#L8355
// ```
// aggr_args:
// | '(' aggr_args_list ORDER BY aggr_args_list ')'
// ```
if integer.unwrap() == 1 {
tokens.push(TokenProperty::from(Token::Order));
tokens.push(TokenProperty::from(Token::By));
}
}
}
},
"DefineStmt" => quote! {
tokens.push(TokenProperty::from(Token::Create));
if n.replace {
Expand All @@ -499,26 +525,8 @@ fn custom_handlers(node: &Node) -> TokenStream {
if node.node.is_none() {
// if first element is a Node { node: None }, then it's "*"
tokens.push(TokenProperty::from(Token::Ascii42));
} else if let Some(node) = &node.node {
if let NodeEnum::List(_) = node {
// there *seems* to be an integer node in the last position of args that
// defines whether the list contains an order by statement
let integer = n.args.last()
.and_then(|node| node.node.as_ref())
.and_then(|node| if let NodeEnum::Integer(n) = node { Some(n.ival) } else { None });
if integer.is_none() {
panic!("DefineStmt of type ObjectAggregate has no integer node in last position of args");
}
// if the integer is 1, then there is an order by statement
// BUT: the order by tokens should be part of the List or maybe
// even the last FunctionParameter node in the list
if integer.unwrap() == 1 {
tokens.push(TokenProperty::from(Token::Order));
tokens.push(TokenProperty::from(Token::By));
}
}
}
}
} }
// if its a list, we handle it in the handler for `List`
},
_ => panic!("Unknown DefineStmt {:#?}", n.kind()),
}
Expand Down
6 changes: 3 additions & 3 deletions crates/codegen/src/get_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn get_nodes_mod(proto_file: &ProtoFile) -> proc_macro2::TokenStream {
let root_node_idx = g.add_node(Node {
kind: SyntaxKind::from(node),
depth: at_depth,
properties: get_node_properties(node),
properties: get_node_properties(node, None),
location: get_location(node),
});

Expand All @@ -45,12 +45,12 @@ pub fn get_nodes_mod(proto_file: &ProtoFile) -> proc_macro2::TokenStream {
NodeEnum::BitString(n) => true,
_ => false
} {
g[parent_idx].properties.extend(get_node_properties(&c));
g[parent_idx].properties.extend(get_node_properties(&c, Some(&node)));
} else {
let node_idx = g.add_node(Node {
kind: SyntaxKind::from(&c),
depth: current_depth,
properties: get_node_properties(&c),
properties: get_node_properties(&c, Some(&node)),
location: get_location(&c),
});
g.add_edge(parent_idx, node_idx, ());
Expand Down