Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pass parent to get_node_properties and add support for create aggregate with order by #72

Merged
merged 2 commits into from
Dec 17, 2023

Conversation

psteinroe
Copy link
Collaborator

What kind of change does this PR introduce?

adds support for CREATE AGGREGATE

What is the current behavior?

panics

What is the new behavior?

still panics for aggregates with ORDER BY

Additional context

follow-up for #69

Comment on lines 513 to 519
// 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));
}
}
Copy link
Collaborator Author

@psteinroe psteinroe Dec 14, 2023

Choose a reason for hiding this comment

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

@cvng I tried it myself and even though I figured how to know what tokens to add, it does still fail because the ORDER BY tokens must be part of either the List node or the last FunctionParameter node.

as of now, I don't see a way around this other than passing the node_graph and figuring out the nodes for the FunctionParameter from its parents...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

its not a big deal, but I hoped that the AST is "clean enough" for this to not be required...

Copy link
Collaborator Author

@psteinroe psteinroe Dec 14, 2023

Choose a reason for hiding this comment

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

other valuable option: skip the List node entirely in the parser and put its children directly beneath the DefineStmt node. Could get messy though since the List node is also used elsewhere, so this has to be done only for children of DefineStmt.

Copy link
Contributor

@cvng cvng Dec 14, 2023

Choose a reason for hiding this comment

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

@psteinroe I'm not confortable enough with the AST to know what a proper solution should be. It the test in #69 passes with the same output?

As of my understanding, I would go with solution 3 (direct children to DefineStmt) - based on the docs - aggregate with order by is kind of a special case

The syntax with ORDER BY in the parameter list creates a special type of aggregate called an ordered-set aggregate;

(also, I'm not sure this syntax exists elsewhere)

I let you close the previous PR if you think this is a better approach

Copy link
Contributor

Choose a reason for hiding this comment

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

Just by reading this, one could think that the order by is before / parent (same thing) of the list?

aggr_args:
    | '(' aggr_args_list ORDER BY aggr_args_list ')'

https://github.com/postgres/postgres/blob/REL_15_STABLE/src/backend/parser/gram.y#L8355

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes it should be part of the List node. we should then pass the parent AST node somehow, and for a List node, check if the parent is a DefineStmt, and do the same checks as I implemented for the DefineStmt. passing the node_graph will not be sufficient since we need the sibling token to be resolved first which cannot be guaranteed. we could also just always pass the entire DefineStmt / root node.

Copy link
Contributor

Choose a reason for hiding this comment

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

keeping around a notion of context / parent node for some children is what you suggest? this looks like the approach taken by other parsers working with the Postgres AST - if I fully understand

also is it reasonable to have order by direct child of DefineStmt this case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

keeping around a notion of context / parent node for some children is what you suggest?

yes, at least within get_node_properties. I will add a pr early next week for this.

also is it reasonable to have order by direct child of DefineStmt this case?

no, because its between two second-level children. we would have to close and re-open the list node which is not a valid. and ignoring the List is also not a great solution.

@psteinroe psteinroe changed the title feat: add support for create aggregate with order by feat: pass parent to get_node_properties and add support for create aggregate with order by Dec 17, 2023
@psteinroe psteinroe requested a review from cvng December 17, 2023 12:58
@psteinroe
Copy link
Collaborator Author

psteinroe commented Dec 17, 2023

@cvng we now pass the parent to get_node_properties and the test passes 👍 lmk if we can merge this.

Copy link
Contributor

@cvng cvng left a comment

Choose a reason for hiding this comment

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

sure! I think parent will be required for some other "intermediate" nodes as well @psteinroe

@psteinroe psteinroe merged commit 50248d5 into main Dec 17, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants