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

Remove parenthesis from the AST #5906

Merged
merged 4 commits into from Mar 27, 2020
Merged

Conversation

systay
Copy link
Collaborator

@systay systay commented Mar 9, 2020

To make sure that the meaning of queries didn't change by mistake, Vitess used to keep parens around in the AST even though they are nog strictly speaking needed there.

This change set solves the parens problem by taking precedence into consideration, and adding parens to the output string when needed instead of keeping them in the AST structure.

The benefit is that other parts of the code can just ignore parenthesis and trust that the AST will handle it for them.

NOTE: This introduces a behaviour change that can be noticable for end-users. Unaliased SELECT expressions can get the column name used changed (by parens being removed).

Example:

mysql> select (1)+1;

+-------+
| 1 + 1 |
+-------+
|     2 |
+-------+
1 row in set (0.00 sec)

Vitess already did some changing of column names (white spaces were not kept around). Now it does it a little differently. If you need a specific name, it's advisable to use the AS columnAlias construct.

case 'a':
buf.WriteArg(values[fieldnum].(string))
case 'p':
Copy link
Contributor

Choose a reason for hiding this comment

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

This part feels like it can lead to future bugs. Is there a way to make %v handle this correctly?
The main concern is this: someone may call Myprintf with %v without knowing that they should have called ExprPrintf with %p.

@systay systay force-pushed the remove-parens branch 8 times, most recently from b4e8c10 to 47a57a6 Compare March 27, 2020 08:43
@systay
Copy link
Collaborator Author

systay commented Mar 27, 2020

@sougou I've squashed, rebased and generally cleaned up this PR

Is there anything else you think needs to change before we can merge it?

systay and others added 4 commits March 27, 2020 10:16
Instead of keeping this information in the AST, we put in parens
in the string representation of queries when needed when printing.

This includes adding code that takes precendence into consideration - something
that already we already need to do when parsing. Unfortunately I haven't found
a good way to use the same source information for both sides of the
precedence puzzle.

Signed-off-by: Andres Taylor <andres@planetscale.com>
Myprintf uses from AST now include the parent struct.

In order to get better readability and guarantees that we don't introduce regressions in the future,
this refactoring changes Myprintf uses from the AST to always provide the parent structure. Using
this information, Myprintf can figure out when parens are needed.

Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
@sougou sougou merged commit ffc8279 into vitessio:master Mar 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants