Repository navigation
Conversation
…ntifier_quoting_style fields
- hilite matching bracket - remove accidentally added space - remove unused field in Hiliter
|
I have started implementing merging the FormatSettings, FormatState, and FormatStateStacked together, and now have a couple of questions. (still relevant) 1. API design. In However, there are about ~30 cases when a) Why do these usages need a blank state? I don't need to know that to recreate the behavior, I'm just curious to have an extra context behind this just in case. b) Since we're incapsulating FormatSettings, FormatState, and FormatStateStacked into a new Formatter class, both Just clarifying - in your suggested snipped above there's no longer a need to expose neither UPD On second thought, here are the benefits of keeping both methods: d) We could also decide to hide Formatter for external users, only exposing WriteBuffer and FormatSettings. Then (more like not relevant, I made a decision to pass Formatter by My API aspirations for the Formatter class are to delete all its copy/move c-tors/assignment operators and provide several methods for creating a copy of the Formatter, e.g. The benefit of this approach compared with just copy-constructors is that you can have this: Instead of this: However, since Solution 1. Create a local variable to store a copy of Solution 2. Make format_impl accept (Errr, this is not precisely correct, but should be enough as an idea.) This solution is just as morally wrong as solution 3. Solution 3. I could just take it by r-value reference in (Preferred by me) Solution 4. Make Just to be clear, the Formatter class would look like this: (I kept the |
- Encapsulates FormatSettings, FormatState, FormatStateStacked - Change IAST.format and IAST.formatImpl signatures - 7/450 usages fixed (grep -rn "formatImpl") with dependencies - Committed files pass static checks
- Here all formatImpl overrides call a format* function within itself with just the FormatSettings argument, without the FormatState and FormatStateStacked. We need to make sure we pass a copy of the FormattingBuffer, to erase the state. - TODO(me): check possible behaviour change, since we're keeping FormatState in the FormattingBuffer copy. I.e. check that those format*(FormatSettings) functions do not use or modify FormatState members.
- out.copy() (possibly?) needed when calling other format* methods w/o state
|
TODO:
No decisions needed:
|
|
TODO: modify my code according to this description. My last few commits are wrong! Squashing
|
TestingI'm going to do some regression tests - take all *.SQL test, run old and new There's a caveat -- I've found a few small bugs in formatting, that I need to fix in a separate PR before doing the test. Also, I'll need to implement a 'hiliting bytes merger' to cancel out some hiliting bytes. E.g. if there were a line like this |
# Conflicts: # src/Parsers/ASTColumnsTransformers.cpp # src/Parsers/ASTConstraintDeclaration.h # src/Parsers/ASTCreateQuery.cpp # src/Parsers/ASTCreateQuery.h # src/Parsers/ASTDeleteQuery.cpp # src/Parsers/ASTDictionary.h # src/Parsers/ASTFunction.cpp # src/Parsers/ASTIndexDeclaration.h # src/Parsers/ASTProjectionDeclaration.h # src/Parsers/ASTRenameQuery.h # src/Parsers/ASTSelectQuery.cpp # src/Parsers/ASTSetQuery.cpp # src/Parsers/ASTTableOverrides.h # src/Parsers/Access/ASTSetRoleQuery.h # src/Parsers/Access/ASTShowGrantsQuery.h # src/Parsers/IAST.h
|
Too many hiliting sequences. For example, let's consider the query And before your changes it was just The test 02105_backslash_letter_commands failed because of that. |
| void IAST::FormattingBuffer::writePossiblyHilited(std::string_view str, const char * hilite_type) const | ||
| { | ||
| Hiliter hiliter = createHiliter(hilite_type); | ||
| ostr << str; |
There was a problem hiding this comment.
A possible optimization: instead of writing a closing hiliter in ~Hiliter FormattingBuffer can remember the last opening hiliter and check it with next output. For example, if you have a sequence of writeKeyword() it would allow to write only one hilite_keyword and only one hilite_none.
There was a problem hiding this comment.
I was thinking about but introducing a state machine would complicate the logic for no gain, as there's no need for the optimisation of hilite. There are so many public methods which will need to flush the buffer.
And if anyone ever goes back to implementing the original task of limiting line length, then it will become even more complicated, as it will probably need a state machine of its own.
There was a problem hiding this comment.
And if anyone ever goes back to implementing the original task of limiting line length, then it will become even more complicated, as it will probably need a state machine of its own.
What is the original task?
There was a problem hiding this comment.
There was a problem hiding this comment.
In regard to #38317 I still don't see why it's better to format
\u001b[1mSHOW \u001b[0m\u001b[1m\u001b[0m\u001b[1mTABLES\u001b[0m
instead of
\u001b[1mSHOW TABLES\u001b[0m
In order to limit the length of a line the characters must be counted without hilite sequences anyway, I suppose. So FormattingBuffer needs to memorize the current number of characters in the current line. And I don't think there is a lot of difference here between memorizing one or two variables.
There was a problem hiding this comment.
I feel like it's a premature optimisation. It doesn't affect performance, it only complicates the code logic.
I agree that squashing hilites together does improve readability for debugging, though. But it's a very abstract scenario.
However, I just realised that implementing squashing might over-complicate the whole PR. Earlier we agreed that I'll encapsulate FormattingBuffer.ostr in a separate PR because it affects ~1000 lines.
To summarize, here are pros for squashing hilites:
- easier readability for debugging
- XX% fewer bytes per hilited query (but does anyone care?)
Cons:
- complicates the code (additional state machine)
- complicates the PR (encapsulating
ostrhere) - I'll need to write more code instead of keeping everything as is :)
The question is, do you feel like the pros outweigh the cons? I'm honestly not very excited to write even more code at this point, and I feel like just merging this PR as is would already bring value.
There was a problem hiding this comment.
complicates the PR (encapsulating ostr here)
We can encapsulate ostr in a half-way manner:
class FormattingBuffer
{
public:
~FormattingBuffer() { setHilite(hilite_none); }
WriteBuffer & out() { setHilite(hilite_none); return ostr; }
void writeKeyword(std::string_view str) const
{
if (keyword.empty())
return;
setHilite(hilite_keyword);
ostr << str;
}
private:
WriteBuffer & ostr;
...
};
and all will need to do is to mechanically replace every .ostr with .out(). It doesn't seem like a difficult task.
I feel like it's a premature optimisation. It doesn't affect performance, it only complicates the code logic.
I'm not 100% sure about performance, but anyway. We format ASTs very often, it's not like one-time task. So it would be nice to be accurate with that.
| @@ -1 +0,0 @@ | |||
| [1mCREATE USER[0m user[1m IDENTIFIED[0m[1m WITH plaintext_password[0m[1m BY[0m 'hello' | |||
There was a problem hiding this comment.
Let's restore this test. Later we can think about removing it but currently it's better to see the changes in hiliting this PR makes.
There was a problem hiding this comment.
I've moved this test here
where it will be tested with a hilite-aware comparator
There was a problem hiding this comment.
It's not exactly the same, so I think it's better to keep both tests for now.
| void nlOrWs() const; // Write newline or whitespace. | ||
| void nlOrNothing() const; | ||
|
|
||
| void writeSecret(const String & secret = "") const; |
There was a problem hiding this comment.
Let's use std::string_view for such arguments always in this class where it doesn't cause troubles.
Here I just tried to make my comment more readable, the test's output was like |
|
@vitlibar This is a good pull request, and we need it. |
Ok, I'll do rebasing of the PR and maybe also correct it a little bit. |
|
@alexey-milovidov @vitlibar I'd like to finish it myself, I'll get reactivated and finish it up :) @vitlibar I'll do the hilite squishing, as you insist. |
|
Thank you! I will be very happy to have this merged. |
|
Dear @murfel, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message. |
I'll continue working on this PR. |
|
Dear @murfel, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message. |
|
We need this! |
Template
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
IAST::FormatSettings refactoring to encapsulate its fields. Now proper API added to be used mostly within IAST::formatImpl, primarily used for clickhouse-format.
Documentation entry for user-facing changes
My comments
#45534
This PR will consist of several commits, each commit will encapsulate one field at a time. Each commit is supposed to keep the build stable.
Accidentally fixed issues
show_secretswas missed in semi-copy-ctor - c0bcf98