-
Notifications
You must be signed in to change notification settings - Fork 187
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
Preserve comments #15
Comments
@mdeland Thats a good question - I think it would unfortunately be quite invasive to patch up the parser. It seems PostgreSQL treats comments as whitespace in the lexical scanner: https://github.com/postgres/postgres/blob/master/src/backend/parser/scan.l#L201 Since pg_query just takes that same code, it has to live within those constraints. Whats the use case for you? I assume we might actually have an easier time using a regular expression to find the comments (and we could look into making that easier, if helpful). |
Yeah, I assumed the scanner was probably just ignoring the comments since no internal PG node representation represents a comment. I am trying to use the output here to format sql, but it would be nice to preserve the comments interspersed where they fall within the query. |
@mdeland Yeah, that makes sense. I don't think there's an easy way to support this as it stands - I'd be willing to integrate a small parser patch, since I'm already carrying one to support parsing Do you think you'd be interested in diving into the C code yourself and see if you can make this work? (I'm too short on time right now to do so) |
I'll take a look at it - I'm not familiar with the bison (?) parser framework but could be interesting. |
@mdeland Yeah, I'll warn you though - it can be a bit painful :-) Note also that we want to avoid making any invasive changes (i.e. changing the way things work too much), since this needs to be synced with every new PostgreSQL release. In any case, have fun! |
Just adding a note here that Also note that pg_hint_plan is licensed under BSD-3-clause, so if we do end up using some of their code we should attribute accordingly. |
Closing this, since this has been implemented in the scanner method released in pg_query 2.0: (note it doesn't really fit the logic to put this information in the parser, so you would have to use both the scanner and the parser method in some cases) |
Is there anyway way to preserve comments in the output of the queryparser? It seems that comments aren't part of the pg parsetree, but it would be nice to be able to preserve their content/location somehow.
The text was updated successfully, but these errors were encountered: