Skip to content

Update unions with named node & allow more optional paranthesis - #178

Merged
DerekStride merged 4 commits into
mainfrom
ds/optional-parens
Jul 6, 2023
Merged

Update unions with named node & allow more optional paranthesis#178
DerekStride merged 4 commits into
mainfrom
ds/optional-parens

Conversation

@DerekStride

@DerekStride DerekStride commented Jul 4, 2023

Copy link
Copy Markdown
Owner

What

fixes #176

Commits might be easier to review separately

  • New optional_parenthesis & wrapped_in_parenthesis functions to make it easier work with parenthesis
  • Alias custom create_query node with _dml_read
  • New union & intersection set_operation node instead of having custom rules in _select_statement

@DerekStride
DerekStride requested review from dmfay and matthias-Q July 4, 2023 15:37
@DerekStride DerekStride self-assigned this Jul 4, 2023
Comment thread grammar.js Outdated
choice(
$._select_statement,
$.union,
$.intersection,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

there's also an except set operation that yields all tuples in a not present in b

@DerekStride DerekStride Jul 4, 2023

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I had merged that with intersection should these all be grouped under a common set_operation node instead? Or swap back to making them choices under the _select_statement?

SELECT 1 UNION SELECT 2;
Current Option on `main` branch
(statement
  (select
    (keyword_select)
    (select_expression
      (term value: (literal))))
  (keyword_union)
  (select
    (keyword_select)
    (select_expression
      (term value: (literal))))))
Current Option in the PR
(statement
 (union
   (select
     (keyword_select)
     (select_expression
       (term value: (literal))))
   (keyword_union)
   (select
     (keyword_select)
     (select_expression
       (term value: (literal)))))))
Alternative
(statement
 (set_operation
   (select
     (keyword_select)
     (select_expression
       (term value: (literal))))
   operation: (keyword_union)
   (select
     (keyword_select)
     (select_expression
       (term value: (literal)))))))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

oh I like having the generic node type discriminable by the operation field in the third!

Comment thread grammar.js
$._dml_read,
')',
),
optional_parenthesis($._dml_read),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

okay it's really weird that you can wrap a select but not an insert or a delete

@matthias-Q matthias-Q Jul 4, 2023

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@dmfay did you tested that? It is really astonishing how many things are not documented in postgres

Update: I tested it, and yes it is not possible to have parens around delete/insert 😲

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

yeah even if you use a returning it refuses to parse any parenthesized write statement 🤯

Comment thread test/corpus/cte.txt
(identifier)
(keyword_as)
(statement
(statement

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

missing a possible test for cte in parentheses -- this parses in Postgres:

((((with x as ((((select * from ints)))) ((((select * from x))))))));

@DerekStride DerekStride Jul 4, 2023

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I updated the CTE's to be allowed to be wrapped in parenthesis & added a tests but an arbitrary number of parenthesis might require a custom scanner.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

dollar quotes (beyond the few common types I added explicitly) need a custom scanner too, but I didn't have much luck with that when I tried a while back

Comment thread grammar.js
}

function optional_parenthesis(node) {
return prec.right(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

does it not work without right-precedence?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Nope, I think it's because there's multiple layers where the parenthesis are allowed to appear so this pushes it "down" the tree.

$ tree-sitter generate
Unresolved conflict for symbol sequence:

  '('  union  •  ')'  …

Possible interpretations:

  1:  '('  (_dml_read  union)  •  ')'  …
  2:  (_dml_read  '('  union  •  ')')

Possible resolutions:

  1:  Specify a left or right associativity in `_dml_read`
  2:  Add a conflict for these rules: `_dml_read`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ah weird, that's probably it. Not a lot to do about it I suppose

@matthias-Q matthias-Q left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Wow, many changes. LGTM, I hope nothing breaks 😄

Maybe add this extra function that I suggested in a comment.

Comment thread grammar.js
@DerekStride

Copy link
Copy Markdown
Owner Author

I added a few more commit to address some PR comments I also have an open question here: #178 (comment)

I'd appreciate another round of reviews 🎉

@DerekStride
DerekStride requested a review from matthias-Q July 4, 2023 19:04
@DerekStride
DerekStride force-pushed the ds/optional-parens branch from 458d107 to 5b44a8f Compare July 5, 2023 14:31
@DerekStride
DerekStride force-pushed the ds/optional-parens branch from 5b44a8f to 5c01247 Compare July 5, 2023 20:20

@matthias-Q matthias-Q left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

So many changes. Since it is now mostly using the new functions I would so go for it. looks good!

@DerekStride
DerekStride merged commit 77ddfc9 into main Jul 6, 2023
@DerekStride
DerekStride deleted the ds/optional-parens branch July 6, 2023 13:17
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.

unions after cte aren't parsed properly

3 participants